My code:
class MyPromise extends Promise {
constructor(func) {
super(func);
}
SomePromiseExtensionMethod() {
// do stuff...
}
}
let test = async function() {
let promise = new MyPromise(function (resolve) {
console.log('foo');
resolve();
});
console.log('start printing:');
await promise;
};
test();
I'm leaning Promise
and want to create a class which derived from Promise
class.
My question: Is there a way to delay printing foo
until I call await promise
?
UPDATE:
I want to create this class because I want to run another methods while the object is a Promise
. (Just like: mypromise.SomePromiseExtensionMethod()
).
UPDATE 2: My real project:
let Task = (function () {
let __data = {};
__data.Run = function (...args) {
if (args.length) {
let func = args[0];
return new Task(func);
}
};
class Task extends Promise {
constructor(action) {
super(action);
}
static set Run(value) {
return __data.Run;
}
static get Run() {
return (...args) => __data.Run.call(this, ...args);
}
}
return Task;
}());
let test = async () => {
let task = Task.Run(r => r(console.log('foo')));
console.log('start printing');
await task;
};
test();