在我的代码中,Promise.all()一旦所有的承诺都完成了,我就会异步运行代码.有时,一个承诺会失败,我不知道为什么.我想知道哪个承诺失败了.传递一个回调作为第二个参数的.then方法没有太大的帮助,因为我知道那一个承诺被拒绝而不是其承诺被拒绝.
堆栈跟踪也没有帮助,因为第一项是Promise.all()错误处理程序.Error传递给传递给try函数的第二个函数的第一个参数的对象Promise.all()的行号只是我记录行号的行号.
有谁知道找出哪个承诺拒绝的方法?
- 编辑 -
我最近遇到了一些关于承诺的奇怪的事情,但我想这可能是因为它违背了承诺的哲学.
考虑以下代码:
// Assuming Auth is just a simple lib doing http requests with promises
Auth.signup()
.then(succCall, errCall)
.then(loginSucc, loginErr)
// My callbacks here
function succCall (){
// OK, send second promise
console.log('succCall');
return Auth.login();
}
function errCall(){
// I do some things here and now
// I want to break out from here
console.log('errCall');
}
function loginSucc(){
// This is the callback of the login method when it went OK
// I want to enter here …Run Code Online (Sandbox Code Playgroud) 我在node 4.3脚本中有一个函数链,类似于回调 - >承诺 - > async/await - > async/await - > async/await
像这样:
const topLevel = (resolve, reject) => {
const foo = doThing(data)
.then(results => {
resolve(results)
})
.catch(err => {
reject(err)
})
}
async function doThing(data) {
const thing = await doAnotherThing(data)
return thing
}
async function doAnotherThing(data) {
const thingDone = await etcFunction(data)
return thingDone
}
Run Code Online (Sandbox Code Playgroud)
(之所以不async/await完全是因为顶级函数是一个任务队列库,表面上不能运行async/await样式)
如果etcFunction()抛出,error泡沫一直上升到顶层Promise吗?
如果没有,我怎么能冒泡errors?我需要每个包装await中try/catch …
我不一定要错,但我有:
getFromDb().then (tradeData) ->
if not tradeData
# DO NOT CONTINUE THE CHAIN
else
getLatestPrice tradeData
.then (latestPrice) ->
...
.then ->
...
.then ->
...
.catch (err) ->
next err
Run Code Online (Sandbox Code Playgroud)
如果没有tradeData,我有什么方法可以中断链?
有什么区别
?
因为这两种JavaScript表达式总是产生相同的结果,无论是内容和状态myPromise和功能的实现a和b?
除了代码可读性之外,我是否应该更喜欢使用一个而不是另一个?
这是我在单元测试时遇到的一个设计问题。\n让我们深入研究示例:
\n想象一下:
\nasync function foo()\xc2\xa0{\n try {\n return apiCall()\n }\n catch (e) {\n throw new CustomError(e);\n } \n}\n\n\n\nasync function bar() {\n return foo()\n}\n\n\n\nasync function main() {\n try {\n await bar()\n }catch(e) {\n console.error(e)\n }\n}\n\nmain()\nRun Code Online (Sandbox Code Playgroud)\n我们在这里看到什么?唯一没有 try-catch 块的函数是 bar。\n但是如果 foo 失败,它应该被 main catch 捕获。
\n虽然像这样进行单元测试
\ndescribe(\'testing bar\', () => {\n it(\'foo should throw\', () => {\n foo.mockImplementantion(() => { throw new CustomError(\'error\')});\n bar()\n .then((result) => console.log(result))\n .catch((err) => { exepect(err).toBeInstanceOf(CustomError)}) // this is what we are …Run Code Online (Sandbox Code Playgroud) 我希望在我的团队的代码库中标准化Q promises的使用.在承诺方面,是否有任何好的jscs扩展(或其他短语)来帮助强制执行风格?
我们希望我们的承诺遵循以下形式:
promise()
.then()
.catch()
.done();
Run Code Online (Sandbox Code Playgroud)
并希望.then()在我们的代码中捕获任何缺少a的内容.catch()
对于承诺的其他风格提示的建议也是受欢迎的.
我想扩展Promise和更改then签名,以便其回调接收两个值.我尝试了不同的方法,其中两个在此处记录和测试.可悲的是,我得到了各种错误,或者由此产生的类不像Promise.
方法1:包装本机承诺
export class MyWrappedPromise {
constructor(data) {
this.data = data;
this.promise = new Promise(evaluate.bind(data));
}
then(callback) {
this.promise.then(() => callback(this.data, ADDITIONAL_DATA));
}
catch(callback) {
this.promise.catch(callback);
}
}
Run Code Online (Sandbox Code Playgroud)
方法2:扩展本机承诺
export class MyExtendedPromise extends Promise {
constructor(executor, data) {
super(executor);
this.data = data;
}
static create(data) {
return new MyExtendedPromise(evaluate.bind(data), data);
}
then(callback) {
return super.then(() => callback(this.data, ADDITIONAL_DATA));
}
}
Run Code Online (Sandbox Code Playgroud)
有没有人对我做错了什么有任何建议?随意在GitHub上创建PR.
谢谢
-------------------编辑---------------------
一些额外的代码和信息使得上面的代码更容易理解,而无需查看Github上的代码和测试.
evaluate只是Promise执行者函数.我把它解压出来,这样我就可以在所有的实现和测试中保持一致.它可能看起来很复杂,但它的结构是模拟我的"真实"项目.
export function evaluate(resolve, reject) {
const …Run Code Online (Sandbox Code Playgroud) 通常,当涉及到 JavaScript 中async/ await 的错误处理时,人们默认使用try/ catch。但我想知道我是否可以使用.catch(),如
const res = await fetch().catch((error) => (
// error handling
));
const json = await res.json();
Run Code Online (Sandbox Code Playgroud)
try我想知道这是否与/catch块相同
try {
const res = await fetch()
const json = await res.json();
} catch(e) {
// error handling
}
Run Code Online (Sandbox Code Playgroud)
我知道从技术上讲try/catch块也可以捕获从中引发的错误res.json();,所以我想它仍然比示例更可取.catch()?
我对 async/await 很陌生,想知道使用 async/await 重构以下代码的最佳方法是什么?
export const createUser = (values, history) => {
return dispatch => {
axios.post('/api/signup', values)
.then(res => {
console.log('result', res);
}, rej => {
console.log('rejection', rej);
});
}
}
Run Code Online (Sandbox Code Playgroud)
当只提供一个参数时,.then对我来说非常简单,但是如果你有两个像这里这样的参数会发生什么?
javascript ×10
promise ×6
async-await ×4
es6-promise ×4
node.js ×3
angularjs ×1
bluebird ×1
coding-style ×1
jestjs ×1
jscs ×1
q ×1