Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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 在调用承诺链后调用Q承诺函数_Javascript_Node.js_Promise_Q - Fatal编程技术网

Javascript 在调用承诺链后调用Q承诺函数

Javascript 在调用承诺链后调用Q承诺函数,javascript,node.js,promise,q,Javascript,Node.js,Promise,Q,我正在使用Q promise库的Node.js应用程序中工作。我有两组承诺链,一个用于控制流,另一个用于调用服务方法,我从中检索数据,我的问题是,我需要获取承诺链对另一个承诺链的返回值 MyExample.js bookService.getBookById(bookId) .then(bookDetals) .then(function(returnValue) { // <- (A) res.send(200, returnValue); // <- (C)

我正在使用Q promise库的Node.js应用程序中工作。我有两组承诺链,一个用于控制流,另一个用于调用服务方法,我从中检索数据,我的问题是,我需要获取承诺链对另一个承诺链的返回值

MyExample.js

bookService.getBookById(bookId)
  .then(bookDetals)
  .then(function(returnValue) { // <- (A)
    res.send(200, returnValue); // <- (C)
    return returnValue;
  }).catch(function(error) {
    logger.error('Error getting values');
    res.send(500, error);
  });


bookDetals = function(book) {
  myService.retrieveATypeData(book, bookId)
    .then(function(bookData) {
      myService.retrieveBTypeData(bookId)
        .then(function(bdata) {
          bookData[bTypeData] = bdata;
          myService.retrieveCTypeData(bookId)
            .then(function(cdata) {
              bookData[cTypeData] = cdata;
            }).done(function() {
              return bookData; // <- (B)
            })
        });
    });
};
bookService.getBookById(bookId)
.然后(书籍目录)
。然后(函数(returnValue){/您需要返回一个:

bookDetals=函数(book){
返回Q.Promise(功能(解决、拒绝、通知){
myService.retrieveTypeData(book,bookId)
.then(函数(bookData){
myService.retrieveBTypeData(bookId)
.then(函数(bdata){
bookData[b类型数据]=b数据;
myService.retrieveCTypeData(bookId)
.then(功能(cdata){
bookData[cTypeData]=cdata;
}).done(函数(){

resolve(bookData);//因为您使用的是Node,所以我会转向ES6 Promissions。如果您当前的版本还不支持ES6 Promissions,我建议您切换到一个库()来为您多填充它。使用ES6,您可以执行以下操作:

// mock async promise
const getThing = id => (
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        id
      });
    }, 250);
  })
);

// mock async promise
const getDetailsA = thing => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(Object.assign({}, thing, {
        a: 'purple'
      }));
    }, 250);
  })
};

// mock async promise
const getDetailsB = thing => (
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(Object.assign({}, thing, {
        b: 'monkey'
      }));
    }, 250);
  })
);

// mock async promise
const getDetailsC = thing => (
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(Object.assign({}, thing, {
        c: 'dishwasher'
      }));
    }, 250);
  })
);

getThing('123')
  .then(getDetailsA)
  .then(getDetailsB)
  .then(getDetailsC)
  .then(console.log)
  .catch(console.error);

您的
bookDetals
函数(以及其中的所有回调函数)缺少
return
语句-如果不返回承诺,
then
怎么能等待它?!哦,不要使用
done
-你只想
then
。请参阅或者,最好是只查看
return myService.retrieveATypeData…等等,因为这是一个promise@BenjaminGruenbaum也许你错过了我的c中的评论ode。我编写的函数只是“模拟”一个返回承诺的API,这不被视为反模式。这些函数的实现是合理的,也不被视为反模式。@BenjaminGruenbaum如果您可以删除否决票,那就太好了。
const timeout=ms=>new promise(r=>setTimeout(r,ms))
会将所有函数设置为
const getDetailsC=>thing=>timeout(毫秒)
等等。如果我关注优化我的模拟代码,那就太好了。同样,你没有抓住我回答的要点。你关注的是我的模拟,而不是承诺的链接,这才是问题的实际答案。
// mock async promise
const getThing = id => (
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        id
      });
    }, 250);
  })
);

// mock async promise
const getDetailsA = thing => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(Object.assign({}, thing, {
        a: 'purple'
      }));
    }, 250);
  })
};

// mock async promise
const getDetailsB = thing => (
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(Object.assign({}, thing, {
        b: 'monkey'
      }));
    }, 250);
  })
);

// mock async promise
const getDetailsC = thing => (
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(Object.assign({}, thing, {
        c: 'dishwasher'
      }));
    }, 250);
  })
);

getThing('123')
  .then(getDetailsA)
  .then(getDetailsB)
  .then(getDetailsC)
  .then(console.log)
  .catch(console.error);