iT邦幫忙

0

then裡面要怎麼寫

受益良多,謝謝!
想請問一個問題:

funcA().then(funcB).then(funcC);
的寫法,
如果改成
funcA().then(funcB()).then(funcC());
順序是亂掉的
那如果要傳參數進去,該如何寫呢?

fillano iT邦超人 1 級 ‧ 2019-05-23 14:59:20 檢舉
functionB(a, b, c) {
return function(result) {
//using a, b, c and result
}
}

funcA().then(funcB(a, b, c)).then(funcC());
這樣嗎?
Amigo iT邦新手 5 級 ‧ 2019-05-23 17:47:19 檢舉
抱歉, 請參考 https://ithelp.ithome.com.tw/articles/10194569 的例子, 我本來是在那裡留言...
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
Han
iT邦研究生 1 級 ‧ 2019-05-24 09:49:01
最佳解答
function funcX(input) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('花' + input + '秒做完了')
            resolve(input);
        }, input * 1000)
    });
}


funcX(3)
.then(function(){
    return funcX(1) ;
})
.then(function(){
    return funcX(2) ;
})

照你上面討論的做,是要這樣的效果?


補充一下為何你討論那裡then裡面放funcX(1)不行的原因

因為then裡面參數是要傳callback function
你寫這樣等於是要傳一個funcX(1)的結果進去

Amigo iT邦新手 5 級 ‧ 2019-05-24 09:51:04 檢舉

對了! 感謝您!

Amigo iT邦新手 5 級 ‧ 2019-05-24 11:00:14 檢舉

懂了。funcX(3).then(()=>funcX(1)).then(()=>funcX(2)) 這樣就更簡潔了

0
dragonH
iT邦超人 5 級 ‧ 2019-05-23 12:10:00

這是要問js嗎

看不懂你想問什麼..

codepen

function funcA(input) {
  return new Promise((resolve, reject) => {
    resolve(input + 1);
  });
}
function funcB(input) {
  return new Promise((resolve, reject) => {
    resolve(input + 1);
  });
}
function funcC(input) {
  return new Promise((resolve, reject) => {
    resolve(input + 1);
  });
}

funcA(0).then(responseA => funcB(responseA))
        .then(responseB => funcC(responseB))
        .then(responseC => console.log(responseC));

看更多先前的回應...收起先前的回應...
Amigo iT邦新手 5 級 ‧ 2019-05-23 17:51:05 檢舉

我的意思是像這樣:
function funcX(input) {
return new Promise((resolve, reject) => {
resolve(input + 1);
});
}

//以下這寫法不會照順序...
funcX(0).then(funcX(1))
.then(funcX(2))

dragonH iT邦超人 5 級 ‧ 2019-05-23 18:49:50 檢舉

還是不太懂XD/images/emoticon/emoticon04.gif

你想要 B->C->A

funcB(0).then(responseB => funcC(responseB))
        .then(responseC => funcA(responseC))
        .then(responseA => console.log(responseA));

或者 C->B->A

funcC(0).then(responseC => funcB(responseC))
        .then(responseB => funcA(responseB))
        .then(responseA => console.log(responseA));

都可以啊 這本來就沒有什麼順序

Amigo iT邦新手 5 級 ‧ 2019-05-24 09:26:52 檢舉

舉例:

function funcX(input) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('花' + input + '秒做完了')
            resolve(input);
        }, input * 1000)
    });
}

//我希望照3-1-2的順序,但這樣寫不行
funcX(3).then(funcX(1)).then(funcX(2))

dragonH iT邦超人 5 級 ‧ 2019-05-24 09:53:25 檢舉

像樓下大大說的

你的問題不完全是then

codepen

function funcX(input) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(`花${input}秒做完了`);
            resolve(input);
        }, input * 1000)
    });
}

const main = async () => {
  await funcX(3);
  await funcX(1);
  await funcX(2);  
}
main();

可以用 async await

可以確保收到promise後在往後執行

Amigo iT邦新手 5 級 ‧ 2019-05-24 11:03:34 檢舉

對!你先前
funcA(0).then(responseA => funcB(responseA))
.then(responseB => funcC(responseB))
.then(responseC => console.log(responseC));
這樣也是可以的

dragonH iT邦超人 5 級 ‧ 2019-05-24 11:06:36 檢舉

我後來注意到是你then寫法的問題

上面那個你就當參考吧

不過實務上大多都是用上面那個我想

0
fillano
iT邦超人 1 級 ‧ 2019-05-24 09:32:21

因為你程式沒寫好,所以他實際執行的方式跟你想像的不太一樣。

你的程式實際上並不是真的用then串在一起等到上一個跑玩才跑下一個,而是後兩段幾乎funcX同時執行。也就是看setTimeout什麼時候執行,結果就是根據那個順序,跟then沒直接關係。

看更多先前的回應...收起先前的回應...
Amigo iT邦新手 5 級 ‧ 2019-05-24 09:41:17 檢舉

所以我才問怎麼寫ㄚ

fillano iT邦超人 1 級 ‧ 2019-05-24 10:05:01 檢舉

如果是怎麼寫,我前面就回答了喔。

Amigo iT邦新手 5 級 ‧ 2019-05-24 11:32:50 檢舉

謝謝!但funcB(a, b, c)還是要傳出Promise,架了好幾層...

fillano iT邦超人 1 級 ‧ 2019-05-24 15:08:43 檢舉

習慣就好XD,這是Javascript的日常

我要發表回答

立即登入回答