博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
html form callback,Promise.fromCallback
阅读量:4964 次
发布时间:2019-06-12

本文共 1883 字,大约阅读时间需要 6 分钟。

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);

});

});

转载地址:http://bgqhp.baihongyu.com/

你可能感兴趣的文章
tp3.2 自带的文件上传及生成缩略图功能
查看>>
Angular 入门学习
查看>>
[单选题]条件语句的时候不应该使用哪一种控制结构
查看>>
1049 I Think I Need a Houseboat ACM题答案 java版
查看>>
socket tcp
查看>>
Vuex状态管理
查看>>
[LeetCode] The Maze III 迷宫之三
查看>>
rest_framework-02-权限-内置权限源码流程
查看>>
JavaWeb学习总结第五篇--认识Cookie机制
查看>>
网站运行编译器错误CS1617: 选项“6”对 /langversion 无效;必须是 ISO-1、ISO-2、3、4、5 或 Default...
查看>>
git 删除目录
查看>>
swiper 最后一页继续滑动跳转问题
查看>>
带有停止按钮的计时程序
查看>>
Tomcat: Connector中HTTP与AJP差别与整合
查看>>
使用Pelican在Github(国外线路访问)和Coding(国内线路访问)同步托管博客
查看>>
iOS | Runtime应用
查看>>
Codeforces 898F 字符串hash
查看>>
JVM常用工具使用
查看>>
项目实战房租网 mongdb
查看>>
AngularJS 特性—SinglePage、template、Controller
查看>>