栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用Node.js中的Promise替换回调

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Node.js中的Promise替换回调

使用
Promise
课程

我建议看一下MDN的Promise文档,它们为使用Promises提供了一个很好的起点。另外,我确定在线上会有很多教程。:)

注意: 现代浏览器已经支持Promises的ECMAscript 6规范(请参阅上面的MDN文档),并且我假设您要使用本机实现,而没有第三方库。

至于一个实际的例子…

基本原理如下:

  1. 您的API被称为
  2. 您创建一个新的Promise对象,该对象将单个函数用作构造函数参数
  3. 您提供的函数由基础实现调用,并且该函数具有两个函数-
    resolve
    reject
  4. 完成逻辑后,您可以调用其中之一来完全履行Promise或因错误而拒绝它

这看起来可能很多,所以这里是一个实际示例。

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

使用异步/等待语言功能(Node.js> = 7.6)

在Node.js 7.6中,v8 Javascript编译器已通过async /
await支持
进行了升级。现在,您可以将函数声明为is

async
,这意味着它们会
Promise
在异步函数完成执行后自动返回一个已解析的。在此函数内,您可以使用
await
关键字等待另一个Promise解析。

这是一个例子:

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}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427046.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号