async function in constructor class typescript

The solution for “async function in constructor class typescript” can be found here. The following code will assist you in solving the problem.

// Don’t put the object in a promise, put a promise in the object.

class Foo {
public Ready: Promise.IThenable;
constructor() {

this.Ready = new Promise((resolve, reject) => {
$.ajax(…).then(result => {
// use result
resolve(undefined);
}).fail(reject);
});
}
}

var foo = new Foo();
foo.Ready.then(() => {
// do stuff that needs foo to be ready, eg apply bindings
});
// keep going with other stuff that doesn’t need to wait for foo

// using await
// code that doesn’t need foo to be ready
await foo.Ready;
// code that needs foo to be ready

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in