helpful script for deferred promise
function deferred() {
    var outResolve = function() {}; // will be overwritten
    var outReject = function() {}; // will be overwritten
    var promise = new Promise(function (resolve, reject) {
        outResolve = resolve;
        outReject = reject;
    });
    return {
        promise: promise,
        resolve: outResolve,
        reject: outReject,
    };
}