相关疑难解决方法(0)

Promise.all找到被拒绝的承诺

在我的代码中,Promise.all()一旦所有的承诺都完成了,我就会异步运行代码.有时,一个承诺会失败,我不知道为什么.我想知道哪个承诺失败了.传递一个回调作为第二个参数的.then方法没有太大的帮助,因为我知道一个承诺被拒绝而不是承诺被拒绝.

堆栈跟踪也没有帮助,因为第一项是Promise.all()错误处理程序.Error传递给传递给try函数的第二个函数的第一个参数的对象Promise.all()的行号只是我记录行号的行号.

有谁知道找出哪个承诺拒绝的方法?

javascript promise es6-promise

10
推荐指数
1
解决办法
3953
查看次数

用errorCallback打破Promise"then"链

- 编辑 -

我最近遇到了一些关于承诺的奇怪的事情,但我想这可能是因为它违背了承诺的哲学.

考虑以下代码:

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

javascript promise angularjs angular-promise

9
推荐指数
1
解决办法
876
查看次数

从嵌套的async/await函数中捕获错误

我在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?我需要每个包装awaittry/catch …

javascript node.js async-await ecmascript-2017

9
推荐指数
1
解决办法
3304
查看次数

我可以在蓝鸟Promises的早期打破链条吗?

我不一定要错,但我有:

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 promise bluebird

7
推荐指数
2
解决办法
3668
查看次数

Promise.then(a,b)和Promise.then(a).catch(b)一样吗?

有什么区别

因为这两种JavaScript表达式总是产生相同的结果,无论是内容和状态myPromise和功能的实现ab

除了代码可读性之外,我是否应该更喜欢使用一个而不是另一个?

javascript promise es6-promise

7
推荐指数
2
解决办法
205
查看次数

我应该避免在 Node js 上的每个 async/await 中使用 try catch 吗?

这是我在单元测试时遇到的一个设计问题。\n让我们深入研究示例:

\n

想象一下:

\n
async 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()\n
Run Code Online (Sandbox Code Playgroud)\n

我们在这里看到什么?唯一没有 try-catch 块的函数是 bar。\n但是如果 foo 失败,它应该被 main catch 捕获。

\n

虽然像这样进行单元测试

\n
describe(\'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)

javascript node.js async-await jestjs

7
推荐指数
1
解决办法
9089
查看次数

在Javascript中的Linting承诺

我希望在我的团队的代码库中标准化Q promises的使用.在承诺方面,是否有任何好的jscs扩展(或其他短语)来帮助强制执行风格?

我们希望我们的承诺遵循以下形式:

promise()
  .then()
  .catch()
  .done();
Run Code Online (Sandbox Code Playgroud)

并希望.then()在我们的代码中捕获任何缺少a的内容.catch()

对于承诺的其他风格提示的建议也是受欢迎的.

javascript coding-style promise q jscs

6
推荐指数
1
解决办法
700
查看次数

扩展`Promise`并改变`then`签名

我想扩展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 promise es6-promise

6
推荐指数
1
解决办法
81
查看次数

可以将 .catch() 与 async/await 混合使用来进行错误处理吗?

通常,当涉及到 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()

javascript async-await es6-promise

6
推荐指数
1
解决办法
4316
查看次数

如何使用 async/await 重构此函数?

我对 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 node.js async-await

5
推荐指数
1
解决办法
1179
查看次数