I'm trying to familiarise myself with Promises and how they work. While it's a new concept to me I'm relatively sure I can understand most of it. In particular I've been looking at the BlueBird library and working my way through the examples. However there's one code snippet on the page that I can't quite wrap my head around.
Promise.promisifyAll(needle);
var options = {};
var current = Promise.resolve();
Promise.map(URLs, function(URL) {
current = current.then(function () {
return needle.getAsync(URL, options);
});
return current;
}).map(function(responseAndBody){
return JSON.parse(responseAndBody[1]);
}).then(function (results) {
return processAndSaveAllInDB(results);
}).then(function(){
console.log('All Needle requests saved');
}).catch(function (e) {
console.log(e);
});
In this code I understand that the needle
library is being promisified. I think I'd be right in saying that current
is being set to an empty promise.
My question is around the
current = current.then(function () {
return needle.getAsync(URL, options);
});
return current;
code. If needle has been promisified, what's the purpose of nesting it within another promise?