I have to write async function for:
const myAsyncFunction = async(function* (promise) {
const data = yield promise;
console.log(data);
});
myAsyncFunction(Promise.resolve("Hello world")); // console: ‘Hello world!’`
result should be - console: ‘Hello world!’
I thought it would be a right implementation:
function async(cb) {
return cb().next();
}
const myAsyncFunction = async(function* (promise) {
const data = yield promise;
console.log(data);
});
myAsyncFunction(Promise.resolve("Hello world")); // console: ‘Hello world!’`
but I nave a type error: TypeError: myAsyncFunction is not a function
I found some example generator forwards with the results of any promises it has yielded
but I cannt understand how it works and where is my mistake:
function async(cb) {
return function () {
let generator = cb.apply(this, arguments);
function handle(result) {
if (result.done) return Promise.resolve(result.value);
return Promise.resolve(result.value).then(
function (res) {
return handle(generator.next(res));
},
function (err) {
return handle(generator.throw(err));
}
);
}
};
}
please, explain what Im doing wrongly?
solution is