में टाइमआउट मैं नोड.जेएस और टाइपस्क्रिप्ट के साथ हूं और मैं async/await
का उपयोग कर रहा हूं। यह मेरा परीक्षण का मामला है:async/await
async function doSomethingInSeries() {
const res1 = await callApi();
const res2 = await persistInDB(res1);
const res3 = await doHeavyComputation(res1);
return 'simle';
}
मैं समग्र समारोह के लिए समय समाप्त निर्धारित करना चाहते हैं। अर्थात। यदि res1
2 सेकंड लेता है, res2
0.5 सेकंड लेता है, res3
में 5 सेकंड लगते हैं, मुझे टाइमआउट करना होगा कि 3 सेकंड के बाद मुझे एक त्रुटि फेंक दें।
एक सामान्य setTimeout
कॉल के साथ एक समस्या है क्योंकि गुंजाइश खत्म हो जाता है है:
async function doSomethingInSeries() {
const timerId = setTimeout(function() {
throw new Error('timeout');
});
const res1 = await callApi();
const res2 = await persistInDB(res1);
const res3 = await doHeavyComputation(res1);
clearTimeout(timerId);
return 'simle';
}
और मैं सामान्य Promise.catch
साथ इसे पकड़ नहीं कर सकते हैं:
doSomethingInSeries().catch(function(err) {
// errors in res1, res2, res3 will be catched here
// but the setTimeout thing is not!!
});
कैसे हल करने पर कोई भी विचार?
क्या आप एक विशिष्ट वादा पुस्तकालय का उपयोग कर रहे हैं? – Bergi
नहीं, बस मानक वादा। – nkint
तो 2 + 0.5 + 5 + 3 11.5 सेकंड का टाइमआउट बनाता है? – Bergi