Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 理解Promise.fromCallback_Javascript_Promise_Bluebird - Fatal编程技术网

Javascript 理解Promise.fromCallback

Javascript 理解Promise.fromCallback,javascript,promise,bluebird,Javascript,Promise,Bluebird,我在读一篇文章 我遇到了Promise.fromCallBack。我通读了这篇文章,但未能完全理解 有人能解释Promise.fromCallBack做什么以及何时使用它吗?回调函数源自一种称为函数式编程的编程范式。在基本层面上,函数式编程指定函数作为参数的使用 回调函数,也称为高阶函数,是作为参数传递给另一个函数的函数(我们称这个函数为“otherFunction”),回调函数在otherFunction内部调用(或执行)。回调函数本质上是一种模式(针对常见问题的既定解决方案),因此,回调函数

我在读一篇文章 我遇到了
Promise.fromCallBack
。我通读了这篇文章,但未能完全理解


有人能解释Promise.fromCallBack做什么以及何时使用它吗?

回调函数源自一种称为函数式编程的编程范式。在基本层面上,函数式编程指定函数作为参数的使用

回调函数,也称为高阶函数,是作为参数传递给另一个函数的函数(我们称这个函数为“otherFunction”),回调函数在otherFunction内部调用(或执行)。回调函数本质上是一种模式(针对常见问题的既定解决方案),因此,回调函数的使用也称为回调模式

let promise=doSomething();

承诺。然后(成功回调,失败回调)回调函数源自一种称为函数式编程的编程范式。在基本层面上,函数式编程指定函数作为参数的使用

回调函数,也称为高阶函数,是作为参数传递给另一个函数的函数(我们称这个函数为“otherFunction”),回调函数在otherFunction内部调用(或执行)。回调函数本质上是一种模式(针对常见问题的既定解决方案),因此,回调函数的使用也称为回调模式

let promise=doSomething();
承诺。然后(成功回调,失败回调)TL;DR

。如果您希望在运行时将函数包装为承诺,则可以使用此选项

const Promise = require('bluebird')
const User = require('./models/user')

return Promise.fromCallback((cb) => {
  User.findById(ID, cb)
})
  .then(user => console.log(user))
基本上

 const Promise = require('bluebird')
 const User = require('./models/user')

 return // the result of User.findById(ID) in a resolver function 
  .then(user => console.log(user))
较长版本:

基于,
Promise.fromCallback
是使用
Promise((解析,拒绝))

基于,上面的代码为
User.findById(ID,callback)
创建了一个承诺,只是将其包装在解析器函数
(resolve,reject)=>{}

User.findById(ID,resultCallback)
表示:

User.findById(ID).then((user) => {...}).catch((err) => {}) 
.then
部分由
resolve()
创建,而
.catch
部分由
reject()
创建。现在,解析器基本上创建了
reject
resolve
函数

return Promise.fromCallback((cb) => {
  User.findById(ID, cb)
})
  .then(user => console.log(user))
从博客中,我们可以看到,
promise.fromCallback
传入回调函数,而不是手动创建解析器函数

return Promise.fromCallback((cb) => {
  User.findById(ID, cb)
})
  .then(user => console.log(user))
这基本上是将回调的结果封装在承诺中,并解决/拒绝它

TL;DR

。如果您希望在运行时将函数包装为承诺,则可以使用此选项

const Promise = require('bluebird')
const User = require('./models/user')

return Promise.fromCallback((cb) => {
  User.findById(ID, cb)
})
  .then(user => console.log(user))
基本上

 const Promise = require('bluebird')
 const User = require('./models/user')

 return // the result of User.findById(ID) in a resolver function 
  .then(user => console.log(user))
较长版本:

基于,
Promise.fromCallback
是使用
Promise((解析,拒绝))

基于,上面的代码为
User.findById(ID,callback)
创建了一个承诺,只是将其包装在解析器函数
(resolve,reject)=>{}

User.findById(ID,resultCallback)
表示:

User.findById(ID).then((user) => {...}).catch((err) => {}) 
.then
部分由
resolve()
创建,而
.catch
部分由
reject()
创建。现在,解析器基本上创建了
reject
resolve
函数

return Promise.fromCallback((cb) => {
  User.findById(ID, cb)
})
  .then(user => console.log(user))
从博客中,我们可以看到,
promise.fromCallback
传入回调函数,而不是手动创建解析器函数

return Promise.fromCallback((cb) => {
  User.findById(ID, cb)
})
  .then(user => console.log(user))

这基本上是将回调的结果封装在承诺中,并解决/拒绝它

你的问题到底是什么?谁能解释一下
承诺。fromCallBack
做什么以及什么时候使用它。文档非常清楚。在
.promisify()
/
.promisifyAll()
不起作用的情况下,此Bluebird静态方法提供了另一种promisification方法。语法有点麻烦,这就是为什么在大多数情况下,
.fromCallback()
不是您的首选。您的问题到底是什么?有人能解释一下
承诺什么。fromCallback
做什么以及什么时候使用它。文档非常清楚。在
.promisify()
/
.promisifyAll()
不起作用的情况下,此Bluebird静态方法提供了另一种promisification方法。语法有点麻烦,这就是为什么在大多数情况下,
.fromCallback()
不是您的首选。对不起,这个答案中的每一句都是错的。(也没有回答问题)对不起,这个答案中的每一句都错了。(而且根本没有回答这个问题)