promise types code example
Example 1: javascript promise
var promise = new Promise(function(resolve, reject) {
if () {
resolve("Stuff worked!");
}
else {
reject(Error("It broke"));
}
});
promise.then(
function(result) { },
function(error) { }
);
Example 2: type a promise
function test(arg: string): Promise<number> {
return new Promise<number>((resolve, reject) => {
if (arg === "a") {
resolve(1);
} else {
reject("1");
}
});
}
Example 3: typescript promise
new Promise<boolean>((res, rej) => {
res(true);
})
.then(res => {
console.log(res);
return false;
})
.then(res => {
console.log(res);
return true;
})
.then(res => {
console.log(res);
})
.catch(error => {
console.log('ERROR:', error.message);
});