html form callback,Promise.fromCallback

Promise.fromCallback

Promise.fromCallback(

function(function callback) resolver,

[Object {multiArgs: boolean=false} options]

) -> Promise

Promise.fromNode(

function(function callback) resolver,

[Object {multiArgs: boolean=false} options]

) -> Promise

Returns a promise that is resolved by a node style callback function. This is the most fitting way to do on the fly promisification when libraries don't expose classes for automatic promisification by undefined.

The resolver function is passed a callback that expects to be called back according to error-first node conventions.

Setting multiArgs to true means the resulting promise will always fulfill with an array of the callback's success value(s). This is needed because promises only support a single success value while

some callback API's have multiple success value. The default is to ignore all but the first success value of a callback function.

Using manual resolver:

var Promise = require("bluebird");

// "email-templates" doesn't expose prototypes for promisification

var emailTemplates = Promise.promisify(require('email-templates'));

var templatesDir = path.join(__dirname, 'templates');

emailTemplates(templatesDir).then(function(template) {

return Promise.fromCallback(function(callback) {

return template("newsletter", callback);

}, {multiArgs: true}).spread(function(html, text) {

console.log(html, text);

});

});

The same can also be written more concisely with Function.prototype.bind:

var Promise = require("bluebird");

// "email-templates" doesn't expose prototypes for promisification

var emailTemplates = Promise.promisify(require('email-templates'));

var templatesDir = path.join(__dirname, 'templates');

emailTemplates(templatesDir).then(function(template) {

return Promise.fromCallback(template.bind(null, "newsletter"), {multiArgs: true})

.spread(function(html, text) {

console.log(html, text);

});

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值