S
S
Seva2016-12-22 15:37:01
JavaScript
Seva, 2016-12-22 15:37:01

Why is the promise not being resolved?

Actually, here.
Where to dig, what to read? Thank you.

var dc = function(data) {

    return new Promise(function(resolve, reject) {

        var filename = 'Test';

        var contract = function() { ... }

        var policy = function() { ... }

        var invoice = function() { ... }
        
        contract().then(invoice().then(policy().then(function() {
            console.log(filename); // Test
            resolve(filename); // UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): ReferenceError: filename is not defined
        })))
    })
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Risent Veber, 2016-12-22
@risentveber

then(policy().
instead, it may be necessary to write it
. And it is difficult to judge without the code of the contract, policy, invoice functions, besides, if the contract returns a promise, you can do without creating an external promise.
UPDATE: would rewrite like this

function dc(data) {
    var filename = 'Test';

    function contract() {
    	return new Promise(function(resolve, reject) {
    		var stream = ...

     		stream.on('finish', function() {
                console.log('stream is done');
                resolve();
            });

        })
    }

    function policy() {
    	var stream = ...
    	var filename = ...

        return new Promise(function(resolve, reject) {
            stream.on('finish', function() {
                console.log('Policy is done');
                resolve(filename);
            });

        })
    }

    function invoice() {
    	var stream = ...

        return new Promise(function(resolve, reject) {
            stream.on('finish', function() {
                console.log('Invoice is done');
                resolve();
            });
        })
    }

    return contract().then(invoice).then(policy).then(function() {
        console.log(filename);
        return filename;
    }).catch(function(error){ console.error(error); });
}

PS read better YDKJS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question