本文介绍了随后取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

var fetch = require('node-fetch')

function fetchA(){
  fetch('https://github.com/keegoo')
    .then(response => console.log('fetchA'))
    .then(fetchB)
    .then(fetchC)
    .then(() => console.log('hi'))
}

function fetchB(){
  fetch('https://github.com/keegoo/trigger')
    .then(response => console.log('fetchB'))
}

function fetchC(){
  fetch('https://github.com/keegoo/trigger/tree/master/app')
    .then(response => console.log('fetchC'))

}
// call 
fetchA()

在fetchA中,我调用fetchB和fetchC。

Inside fetchA, I called fetchB and fetchC.

我期待输出应该是:

fetchA
fetchB
fetchC
hi

相反,它是:

fetchA
hi
fetchC
fetchB 

为什么?

如果我需要输出为 fetchA - > fetchB - > fetchC - >

If I need the output to be fetchA -> fetchB -> fetchC -> Hi, what should I do?

推荐答案

您的 fetchB fetchC 应该从 fetch 返回承诺,否则后续调用,如 .with(fetchB)立即解决。

Your fetchB and fetchC should return the promises from fetch, otherwise the subsequent calls like .then(fetchB) are resolved immediately.

function fetchB(){
  return fetch('https://github.com/keegoo/trigger')
    .then(response => console.log('fetchB'))
}

function fetchC(){
  return fetch('https://github.com/keegoo/trigger/tree/master/app')
    .then(response => console.log('fetchC'))
}

这篇关于随后取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 00:16