मैं उपयोग कर रहा हूँ async await
के जरिए babel-plugin-transform-async-to-generator
.
शीर्ष स्तर पर मैं एक फ़ंक्शन प्रतिक्रिया की प्रतीक्षा कर रहा हूं। फिर एक और दो async फ़ंक्शन हैं func1
तथा func2
. func2
अतुल्यकालिक सामग्री को पुनः प्राप्त करता है https://www.google.com
.
निम्नलिखित स्क्रिप्ट रिटर्न:
go() called
func1() called
finished
func2() called
func2() called
func2() called
मैं ही कैसे कर सकता हूं console.log("finished")
सभी कॉल सफलतापूर्वक निष्पादित होने के बाद? क्या यह उन्हें स्पष्ट रूप से वापस किए बिना संभव है Promise
संकल्प / अस्वीकार का उपयोग कर रहा है?
यह उदाहरण बहुत सरल है। मैं जो करने की कोशिश कर रहा हूं उसमें पुनरावर्ती फ़ंक्शन कॉल शामिल हैं जो मुझे इंतजार करने के लिए भी कहते हैं
const rp = require("request-promise")
go()
async function go() {
console.log("go() called")
await func1([1,2,3])
console.log("finished the script")
}
async function func1(arr) {
console.log("func1() called")
arr.forEach(async function(element) {
await func2()
})
}
async function func2() {
var res = await rp("https://www.google.com")
console.log("func2() called")
}
उत्तर:
जवाब के लिए 0 № 1मुझे अभी भी यकीन नहीं है कि पुनरावर्ती कॉल के लिए यह कैसे करना है, लेकिन इसके लिए एक अलग प्रश्न / उदाहरण तैयार करेगा।
उपरोक्त उदाहरण का उपयोग करके तय किया जा सकता है for..of
के बजाय forEach
। आंतरिक कार्यों के परिणाम "टी" हो सकते हैं await
ईडी।
const rp = require("request-promise")
go()
async function go() {
console.log("go() called")
await func1([1,2,3])
console.log("finished the script")
}
async function func1(arr) {
console.log("func1() called")
for (let item of arr) {
await func2()
}
}
async function func2() {
var res = await rp("https://www.google.com")
console.log("func2() called")
}