用Node.js中的promises替换回调 [英] Replacing callbacks with promises in Node.js
问题描述
我有一个简单的节点模块,它连接到一个数据库并有几个接收数据的函数,例如这个函数:
I have a simple node module which connects to a database and has several functions to receive data, for example this function:
dbConnection.js:
import mysql from 'mysql';
const connection = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'password',
database: 'db'
});
export default {
getUsers(callback) {
connection.connect(() => {
connection.query('SELECT * FROM Users', (err, result) => {
if (!err){
callback(result);
}
});
});
}
};
模块将从不同的节点模块以这种方式调用:
The module would be called this way from a different node module:
app.js:
import dbCon from './dbConnection.js';
dbCon.getUsers(console.log);
我想使用promises而不是回调来返回数据。
到目前为止,我已经阅读了以下主题中的嵌套承诺:写清洁代码嵌套承诺,但我找不到任何对这个用例来说足够简单的解决方案。
使用承诺返回结果
的正确方法是什么?
I would like to use promises instead of callbacks in order to return the data.
So far I've read about nested promises in the following thread: Writing Clean Code With Nested Promises, but I couldn't find any solution that is simple enough for this use case.
What would be the correct way to return result
using a promise?
推荐答案
使用承诺
类
我建议看看 MDN的Promise文档,它提供了使用Promise的良好起点。或者,我确信网上有很多教程。:)
Using the Promise
class
I recommend to take a look at MDN's Promise docs which offer a good starting point for using Promises. Alternatively, I am sure there are many tutorials available online.:)
注意:现代浏览器已经支持ECMAScript 6 Promises规范(参见上面链接的MDN文档)我假设你想使用本机实现,没有第三方库。
Note: Modern browsers already support ECMAScript 6 specification of Promises (see the MDN docs linked above) and I assume that you want to use the native implementation, without 3rd party libraries.
至于一个实际的例子......
As for an actual example...
基本原理如下:
- 您的API被称为
- 您创建一个新的Promise对象,该对象将一个函数作为构造函数参数
- 您提供的函数由底层实现调用,函数有两个函数 -
解决
和拒绝
- 执行逻辑后,将其中一个调用到要么填写承诺,要么拒绝承认错误
- Your API is called
- You create a new Promise object, this object takes a single function as constructor parameter
- Your provided function is called by the underlying implementation and the function is given two functions -
resolve
andreject
- Once you do your logic, you call one of these to either fullfill the Promise or reject it with an error
这可能看起来很多,所以这里有一个实际的例子。
This might seem like a lot so here is an actual example.
exports.getUsers = function getUsers () {
// Return the Promise right away, unless you really need to
// do something before you create a new Promise, but usually
// this can go into the function below
return new Promise((resolve, reject) => {
// reject and resolve are functions provided by the Promise
// implementation. Call only one of them.
// Do your logic here - you can do WTF you want.:)
connection.query('SELECT * FROM Users', (err, result) => {
// PS. Fail fast! Handle errors first, then move to the
// important stuff (that's a good practice at least)
if (err) {
// Reject the Promise with an error
return reject(err)
}
// Resolve (or fulfill) the promise with data
return resolve(result)
})
})
}
// Usage:
exports.getUsers() // Returns a Promise!
.then(users => {
// Do stuff with users
})
.catch(err => {
// handle errors
})
使用async / await语言功能(Node.js> = 7.6)
在Node.js 7.6中,使用 async / await support 。您现在可以将函数声明为 async
,这意味着它们会自动返回 Promise
,当异步函数完成时,它会被解析执行。在此函数中,您可以使用 await
关键字等待另一个Promise结算。
Using the async/await language feature (Node.js >=7.6)
In Node.js 7.6, the v8 JavaScript compiler was upgraded with async/await support. You can now declare functions as being async
, which means they automatically return a Promise
which is resolved when the async function completes execution. Inside this function, you can use the await
keyword to wait until another Promise resolves.
以下是一个示例:
exports.getUsers = async function getUsers() {
// We are in an async function - this will return Promise
// no matter what.
// We can interact with other functions which return a
// Promise very easily:
const result = await connection.query('select * from users')
// Interacting with callback-based APIs is a bit more
// complicated but still very easy:
const result2 = await new Promise((resolve, reject) => {
connection.query('select * from users', (err, res) => {
return void err ? reject(err) : resolve(res)
})
})
// Returning a value will cause the promise to be resolved
// with that value
return result
}
这篇关于用Node.js中的promises替换回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!