2015-10-07 11 views
7

नहीं कहा जा रहा है मैं वादा लाइब्रेरी ब्लूबर्ड का उपयोग कर रहा हूं और मैं वर्तमान में इस मुद्दे पर चल रहा हूं कि फ़ंक्शन के अंदर सबकुछ बढ़िया हो जाता है, लेकिन जब मैं कोई मान वापस करने का प्रयास करता हूं, तो फ़ंक्शन undefined देता है। , जब foo() बुला यह मूल्य पुस्तक के बजाय undefined हो जाता है:वादे श्रृंखला के अंदर वापसी मूल्य को

function foo() { 
    createGroupMembers(parsedChat).then(function(val) { 
     var members = val; 

     createMessages(parsedChat, maxPages).then(function(val) { 
      var messages = val; 

      Promise.all([ createFrontCover(subject, firstdateOfMessages, lastDateOfMessages, isPreview), createStats(parsedChat), createBackCover(parsedChat)]) 
      .then(function (results) { 
       var front = results[0]; 
       var stats = results[1]; 
       var backcover = results[2]; 

       var book = head + front + stats + members + messages + backcover; 

       console.log('pages in this book: ', pages); 
       console.log(book); // logs perfect values. 

       return book; 
      }); 

     }); 

    }); 
} 

समस्या सरल है:

यह वादा श्रृंखला है। मैं इस व्यवहार का अनुभव क्यों कर रहा हूं?

+2

foo सब –

+0

पर कुछ भी मैं Bluebird से परिचित नहीं हूँ वापस नहीं करता है, लेकिन मैं देशी जे एस वादों का इस्तेमाल किया है। ऐसा कहकर ... अनाम कार्य 'समारोह (परिणाम)' रिटर्न बुक। इसके ऊपर दो अज्ञात फ़ंक्शंस 'फ़ंक्शन (वैल)' कुछ भी नहीं लौटाता है। अंत में, 'foo', कुछ भी वापस नहीं करता है। –

+0

आपको अपने सभी नेस्टेड फ़ंक्शंस को वापस करने की आवश्यकता है। createGroupMembers, createMessages, आदि – ehynds

उत्तर

13
function foo() { 
    return createGroupMembers(parsedChat).then(function(val) { 
     var members = val; 

     return createMessages(parsedChat, maxPages).then(function(val) { 
      var messages = val; 

      return Promise.all([createFrontCover(subject, firstdateOfMessages, lastDateOfMessages, isPreview), createStats(parsedChat), createBackCover(parsedChat)]) 
       .then(function(results) { 
        var front = results[0]; 
        var stats = results[1]; 
        var backcover = results[2]; 

        var book = head + front + stats + members + messages + backcover; 

        console.log('pages in this book: ', pages); 
        console.log(book); // logs perfect values. 

        return book; 
       }); 

     }); 

    }); 
} 

अब foo एक वादा जो

foo().then(function(book) { 
    console.log('huzzah I have book ' + book); 
}); 

ईमानदारी से कहूं तो foo फिर से लिखा जा सकता है पुस्तक के मूल्य के हल कर सकते हैं वापस आ जाएगी, लेकिन यह आपकी जानकारी के लिए एक अलग सवाल पूरी तरह

है: आप foo

function foo() { 
    return createGroupMembers(parsedChat) 
    .then(function(members) { // members 
     return Promise.all([members, createMessages(parsedChat, maxPages)]); 
    }) 
    .then(function(members_messages) { // membersMessages 
     return Promise.all([createFrontCover(subject, firstdateOfMessages, lastDateOfMessages, isPreview), createStats(parsedChat)].concat(members_messages, [createBackCover(parsedChat)])); 
    }) 
    .then(function(results) { // front, stats, members, messages, back 
     var book = head + results.join(''); 

     console.log('pages in this book: ', pages); 
     console.log(book); // logs perfect values. 

     return book; 
    }); 
} 

के साथ इस तरह कुछ ऐसा कर सकता है वह दूसरे में आदेश (केवल आपका था) Promise.all, और .join के रूप में सरल भागों के अंतिम समापन को बनाने के लिए पिछले वादा परिणामों को जोड़ा - ऐसा करने से यह किसी भी त्रुटि को सही ढंग से प्रचारित करेगा, इसलिए आपका उपयोग foo हो सकता है

foo().then(function(book) { 
    console.log('huzzah I have book ' + book); 
}).catch(function(err) { 
    // handle any and all errors here 
}); 
संबंधित मुद्दे