JavaScript / Node.JS在另一个诺言中返回诺言不起作用

我对这些承诺有些困惑,不明白为什么我不能将承诺交还给另一个承诺。 我的问题是以下,我有一个function保存:

save(Driver) { this.exists(Driver).then(exist => { if(exist) { return new Promise(function (resolve, reject) { if (exist === true) { resolve(true); } else if (exist === false) { resolve(false); } else { reject(err); } }); } }); }; 

就像这样简单,当我尝试调用这个函数如下:

 save(driver).then(this.common.saveSuccess(res)) .catch(this.common.noSuccess(res)); 

我得到一个错误,说Cannot read property 'then' of undefined ,我不明白为什么,因为我回来的承诺。 谢谢你的帮助

你的savefunction非常复杂。 你不需要在这里嵌套承诺,只是从this.exists函数返回结果(承诺):

 save(Driver) { return this.exists(Driver); }; 

此外,您使用此function不正确。 save函数可以用true或者false值来parsing,所以你需要在callback函数中validation这个值,并使用catchcallback来查找可能的错误:

 save(driver) .then(exists => { if (exists) { this.common.saveSuccess(res); } else { this.common.noSuccess(res) } }) .catch(err => // process error here);