मैंने नहीं देखा है इन निर्माणों ज्यादा थे, लेकिन मैं अपने आप को उन्हें लिख async का उपयोग करना पाया है/कार्य में का इंतजार है कि आम तौर पर एक वादा वापस नहीं होगा, उदाहरण के लिएES8 तुरंत लागू async समारोह अभिव्यक्ति
chan.consume(queue, (msg) => {
this.pendingMsgs++; // executed immediately
(async() => {
await this.handleMessage(msg);
this.pendingMsgs--;
if (cancelled && this.pendingMsgs === 0) {
await chan.close();
await this.amqpConnectionPool.release(conn);
}
})();
});
रूप
chan.consume(queue, async (msg) => { // external lib does not expect a return value from this callback
this.pendingMsgs++; // executed in promise context(?)
await this.handleMessage(msg);
this.pendingMsgs--;
if (cancelled && this.pendingMsgs === 0) {
await chan.close();
await this.amqpConnectionPool.release(conn);
}
});
या
chan.consume(queue, (msg) => {
this.pendingMsgs++; // no await - excess function decls & nesting
this.handleMessage(msg).then(() => {
this.pendingMsgs--;
if (cancelled && this.pendingMsgs === 0) {
chan.close().then(() => {
this.amqpConnectionPool.release(conn);
});
}
});
});
करने का विरोध किया इस 'एक बात' है? क्या यहां समस्याएं हैं जिनके बारे में मुझे अवगत होना चाहिए? इस तरह की स्थितियों में एसिंक/प्रतीक्षा के उपयोग पर क्या कमी है?
कर रही का फ़ायदा क्या है आप पुस्तकालय के लिए एक वादा लौटने से बचने के लिए चाहते हैं, एक सहायक समारोह है कि कॉलबैक लपेटता बनाने? – Pointy
@ पॉइंट - क्या कुछ स्पष्ट विकल्प है जिसे मैंने अनदेखा किया है? –
@DrewR एक फ़ंक्शन में वापसी जोड़ना जो वर्तमान में कुछ भी वापस नहीं करता है, किसी भी प्रतिस्थापन सिद्धांतों का उल्लंघन नहीं करता है। तो क्यों कार्य और इंडेंटेशन की एक और परत में सब कुछ लपेटने के साथ परेशान? –