[javascript] How do I access previous promise results in a .then() chain?

Another answer, using sequential executor nsynjs:

function getExample(){

  var response1 = returnPromise1().data;

  // promise1 is resolved at this point, '.data' has the result from resolve(result)

  var response2 = returnPromise2().data;

  // promise2 is resolved at this point, '.data' has the result from resolve(result)

  console.log(response, response2);

}

nynjs.run(getExample,{},function(){
    console.log('all done');
})

Update: added working example

_x000D_
_x000D_
function synchronousCode() {_x000D_
     var urls=[_x000D_
         "https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js",_x000D_
         "https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js",_x000D_
         "https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"_x000D_
     ];_x000D_
     for(var i=0; i<urls.length; i++) {_x000D_
         var len=window.fetch(urls[i]).data.text().data.length;_x000D_
         //             ^                   ^_x000D_
         //             |                   +- 2-nd promise result_x000D_
         //             |                      assigned to 'data'_x000D_
         //             |_x000D_
         //             +-- 1-st promise result assigned to 'data'_x000D_
         //_x000D_
         console.log('URL #'+i+' : '+urls[i]+", length: "+len);_x000D_
     }_x000D_
}_x000D_
_x000D_
nsynjs.run(synchronousCode,{},function(){_x000D_
    console.log('all done');_x000D_
})
_x000D_
<script src="https://rawgit.com/amaksr/nsynjs/master/nsynjs.js"></script>
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to scope

Angular 2 - Using 'this' inside setTimeout Why Is `Export Default Const` invalid? How do I access previous promise results in a .then() chain? Problems with local variable scope. How to solve it? Why is it OK to return a 'vector' from a function? Uncaught TypeError: Cannot read property 'length' of undefined Setting dynamic scope variables in AngularJs - scope.<some_string> How to remove elements/nodes from angular.js array Limiting number of displayed results when using ngRepeat A variable modified inside a while loop is not remembered

Examples related to promise

Axios handling errors typescript: error TS2693: 'Promise' only refers to a type, but is being used as a value here Syntax for async arrow function Angular 2: How to call a function after get a response from subscribe http.post How to use fetch in typescript Returning Promises from Vuex actions Use async await with Array.map Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai using setTimeout on promise chain Why is my asynchronous function returning Promise { <pending> } instead of a value?

Examples related to bluebird

How do I access previous promise results in a .then() chain? How do I tell if an object is a Promise? Correct way to write loops for promise. Aren't promises just callbacks? How do I convert an existing callback API to promises?

Examples related to es6-promise

How to reject in async/await syntax? What is difference between Axios and Fetch? What is an unhandled promise rejection? JavaScript ES6 promise for loop Returning Promises from Vuex actions how to cancel/abort ajax request in axios Axios get access to response header fields How to pass parameter to a promise function Wait until all promises complete even if some rejected Handling errors in Promise.all