dimanche 19 avril 2015

Why doesn't this test with promises pass?

I've stepped into wonderful world of Promises a few days ago and I was just thinking I was enlightened. Promises look simple but they can be confusing.


Could you please tell me why the following test doesn't pass?



var Promise = require('bluebird');
var expect = require('chai').expect;
var request = Promise.promisifyAll(require('request'));

describe('Promise', function() {
it('should work again', function() {

var final_result;

function first_promise() {
return new Promise(function(resolve, reject) {
resolve("http://www.google.com");
})
}

function second_promise() {
return new Promise(function(resolve, reject) {
resolve("This is second promise!");
})
}

function inner_async_request(url_from_first_promise) {
return new Promise(function(resolve, reject) {
return request.getAsync(url_from_first_promise).spread(function(response, content) {
final_result = content;
resolve(content);
})
})
}

return request.getAsync('http://127.0.0.1:3000/').spread(function(result, content) {
//do something with content and then return first_promise
console.log(content);
return first_promise;
})
.then(function(url) {
inner_async_request(url).then(function(result) {
console.log(result);
final_result = result;
})
return second_promise;
})
.then(function(result) {
// result should be "This is second promise!"
console.log(result);
// final_result should be google's html
expect(final_result).not.to.be.undefined;
})
});
});


Currently the error is: Unhandled rejection Error: options.uri is a required argument which should be received from first_promise, I guess?


By this test, actually, I want to understand how to use promises which depend on each other AND how to use promises as asynchronous functions inside promises -which should just work separately-.


Thank you


Aucun commentaire:

Enregistrer un commentaire