Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add Promises/await support#90

Open
egormalyutin wants to merge 9 commits into
abbr:master from
egormalyutin:master
Open

Add Promises/await support #90
egormalyutin wants to merge 9 commits into
abbr:master from
egormalyutin:master

Conversation

@egormalyutin

@egormalyutin egormalyutin commented Dec 20, 2017

Copy link
Copy Markdown

Added function deasync.await, that is like await in async function, but works in sync function or global scope.

function sleepAsync(time) {
	return new Promise(function(resolve, reject) {
		setTimeout(function() {
			resolve()
		}, time)
	})
}
async function trim(str) {
	await sleepAsync(2000)
	return str.trim()
}
console.log(deasync.await(trim(' hello ')))

seveibar, rluvaton, milahu, and aminya reacted with thumbs up emoji
Comment thread index.js Outdated

Copy link
Copy Markdown

The throw inside the catch won't really throw outside

Comment thread index.js
Comment on lines +80 to +94
done = false;
result = undefined;

pr
.then(function(r) {
done = true;
return result = r;
})
.catch(function(err) {
done = true
throw err
})

deasync.loopWhile(() => { return !done });
return result;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
done = false;
result = undefined;
pr
.then(function(r) {
done = true;
return result = r;
})
.catch(function(err) {
done = true
throw err
})
deasync.loopWhile(() => { return !done });
return result;
done = false;
result = undefined;
error = undefined;
pr
.then(function(r) {
done = true;
result = r;
})
.catch(function(err) {
done = true;
error = err;
})
deasync.loopWhile(function() { return !done; });
if (error) throw error;
return result;

@SnirBroshi SnirBroshi Nov 15, 2020

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about errors what are falsy? for example Promise.reject(0) or Promise.reject(undefined).
This still would no throw an exception

papb reacted with thumbs up emoji

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NotWearingPants I forgot that those rejections are technically possible. Indeed to cover that it would be better to use a didThrow variable instead.

loynoir commented Nov 10, 2021

Copy link
Copy Markdown
deasync.await = p => deasync((cb)=>p.then(x=>cb(null,x),e=>cb(e,null)))()
> deasync.await(import('some-esm-module'))
[Module: null prototype] {
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

3 more reviewers

@CyrusNajmabadi CyrusNajmabadi CyrusNajmabadi left review comments

@papb papb papb left review comments

@SnirBroshi SnirBroshi SnirBroshi left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /