相关疑难解决方法(0)

如何将现有的回调API转换为承诺?

我想使用promises,但我有一个回调API,格式如下:

1. DOM加载或其他一次性事件:

window.onload; // set to callback
...
window.onload = function() {

};
Run Code Online (Sandbox Code Playgroud)

2.平原回调:

function request(onChangeHandler) {
    ...
}
request(function() {
    // change happened
    ...
});
Run Code Online (Sandbox Code Playgroud)

3.节点样式回调("nodeback"):

function getStuff(dat, callback) {
    ...
}
getStuff("dataParam", function(err, data) {
    ...
})
Run Code Online (Sandbox Code Playgroud)

4.具有节点样式回调的整个库:

API;
API.one(function(err, data) {
    API.two(function(err, data2) {
        API.three(function(err, data3) {
            ...
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

如何在promises中使用API​​,我该如何"宣传"它?

javascript callback node.js promise bluebird

680
推荐指数
12
解决办法
20万
查看次数

在异步函数中,从回调函数返回值返回 Promise(undefined)

我是异步编程的新手,我面临着类似于这个问题的问题,在这个问题中建议的方法使用回调,但我正在尝试使用 Promises 和 async-await 函数来做到这一点。我在控制台中未定义。这是我的例子。我错过了什么?

 //Defining the function
 async query( sql, args ) {
    const rows = this.connection.query( sql, args, async( err, rows ) => 
     { 
        if ( err )
           throw new Error(err); 
        return rows; 
      } );
}

//calling the function here 
 db.query("select 1")
 .then((row) => console.log("Rows",row)) // Rows undefined
 .catch((e) => console.log(e));
Run Code Online (Sandbox Code Playgroud)

javascript callback node.js promise async-await

6
推荐指数
1
解决办法
5183
查看次数

标签 统计

callback ×2

javascript ×2

node.js ×2

promise ×2

async-await ×1

bluebird ×1