简体   繁体   中英

Javascript: Return promise in nested function

I have this code:

createOrUpdate({
    test: 'test'
}).then((response) => {
    console.log(response);
});

which will expect a createOrUpdate to return a promise. Here is the createOrUpdate function:

function createOrUpdate(requestParams) {

    var options = {};

    return getService('global', function(error, serviceResult) {
        if (error) {
            log.error(error);
            return callback(error, null);
        }

        //...

        return requestPromise(options);
    });

}

I have this getService method that takes some paramaters and a call back. I need to get that return requestPromise(options) to return when createOrUpdate is called, but currently it is not working. I get an error saying I cannot call then on what createOrUpdate returns.

Ideas?

Simple, you need to make getService into a promise returning API .

Otherwise, node-callback taking functions don't return anything and ignore their own return values.

function createOrUpdate(requestParams) {

    var options = {};

    return fromCallback(getService)('global').then(options => 
        requestPromise(options);
    );

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM