Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/async await #199

Merged
merged 21 commits into from
Jun 4, 2019
Merged

Refactor/async await #199

merged 21 commits into from
Jun 4, 2019

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented May 30, 2019

Supersedes #189 and includes all of the wonderful work that @zcstarr did in that PR.

All async/await refactors that this relies on have been merged & released. I think this also now addresses all of the PR comments from #189 - can it be merged & released?

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
return setImmediate(() => {
callback(new Error('Not a valid cid'))
})
throw new Error('Not a valid cid')
Copy link
Member

Choose a reason for hiding this comment

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

Needs to be async or users can't delete(cid).catch(console.log) this error.

There's quite a few places where this happens - we should decide if allowing library consumers to use promise syntax is important.

cc @ipfs/wg-js-core

Copy link

Choose a reason for hiding this comment

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

Good point - I think it's important, at least in the places where the documentation says it returns a Promise

Copy link
Member Author

Choose a reason for hiding this comment

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

Marking things async creates a Promise. Best case scenario there's no actual async in the function, but the VM will still put the code after the promise onto the microtask queue which creates a small amount of latency. On hot codepaths this adds up as the Nearform performance analysis of our codebase showed. Our linting rules enforce require-await for this reason (and would cause the suggested change to fail linting).

I take the point about promise syntax - it's not great to have an API where some functions are async and other aren't, but we should not create artificial asynchronicity for the reason outlined above.

Maybe I'm wrong but I'd consider this module an internal API, quite different to js-ipfs itself so the inconsistency bothers me, but not to the point that we should make the code less efficient.

Copy link
Member

Choose a reason for hiding this comment

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

I'm curious to know if await syncFn() has the same effect.

We're always going to call functions that may or may not be async with await right? - because we just don't know if the function will return a promise or not.

So, if await also forces async behaviour then is there a benefit to not using async?

Copy link
Member

@alanshaw alanshaw May 31, 2019

Choose a reason for hiding this comment

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

If the value of the expression following the await operator is not a Promise, it's converted to a resolved Promise.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#Description

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly why you shouldn't just await on everything 😁

FWIW the future-tech version of IPLD figures out if the return value of a function is thenable and resolves it if so (or at least it did in one iteration). Making everything async by default prevents these sorts of optimisations.

Anyway I've updated the PR, everything in the docs that claims to be async is now marked async.

@@ -11,41 +10,34 @@ const LOCKS = {}

/**
* Lock the repo in the given dir.
*
* TODO
Copy link
Member

Choose a reason for hiding this comment

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

What is TODO?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a mystery. Have removed it.

README.md Outdated Show resolved Hide resolved
return setImmediate(() => {
callback(new Error('Not a valid cid'))
})
throw new Error('Not a valid cid')
Copy link

Choose a reason for hiding this comment

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

Good point - I think it's important, at least in the places where the documentation says it returns a Promise

src/index.js Show resolved Hide resolved
src/lock-memory.js Outdated Show resolved Hide resolved
test/options-test.js Outdated Show resolved Hide resolved
src/config.js Show resolved Hide resolved
@achingbrain
Copy link
Member Author

@jacobheun any comments or can this be merged & released?

@jacobheun
Copy link
Contributor

I haven't had a chance to go through this in depth but I also don't need to be a blocker on this since @dirkmc and @alanshaw reviewed.

I can do a merge and minor release of this, the integration with js-ipfs should also help catch any items that may have been missed.

@jacobheun jacobheun merged commit e6db5cf into master Jun 4, 2019
@jacobheun jacobheun deleted the refactor/async-await branch June 4, 2019 10:23
@jacobheun
Copy link
Contributor

@achingbrain v0.27.0 is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants