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

Support Promises #252

Closed
dmihal opened this issue Jan 26, 2018 · 7 comments
Closed

Support Promises #252

dmihal opened this issue Jan 26, 2018 · 7 comments

Comments

@dmihal
Copy link
Contributor

dmihal commented Jan 26, 2018

I would love if the API supported returning Promises as an alternative to callbacks. This would be implemented by returning Promises if no callback is provided.

I'm happy to add the feature and submit a PR if you're open to it!

@jwasinger
Copy link
Contributor

I like this idea. It is backwards compatible as well!

Pending any reservations by @holgerd77 @axic , I would say go ahead :)

@holgerd77
Copy link
Member

Would also very much support this, great that you want to implement it! 😄

Could you post some code snippet with 10LOC or so on how you think/plan this would look like when being called with promises from the outside?

@dmihal
Copy link
Contributor Author

dmihal commented Jan 27, 2018

With promises, the code can be executed in 3 ways:

The existing way with callbacks

vm.runCode({
  code: Buffer.from(code, 'hex'), // code needs to be a Buffer
  gasLimit: Buffer.from('ffffffff', 'hex')
}, function(err, results){
  console.log('returned: ' + results.return.toString('hex'));
})

Using promises

vm.runCode({
  code: Buffer.from(code, 'hex'), // code needs to be a Buffer
  gasLimit: Buffer.from('ffffffff', 'hex')
})
.then(function(results){
  console.log('returned: ' + results.return.toString('hex'));
})
.catch(function(err){
  console.error(err);
})

Or using async/await

try {
  var results = await vm.runCode({
    code: Buffer.from(code, 'hex'), // code needs to be a Buffer
    gasLimit: Buffer.from('ffffffff', 'hex')
  });
  console.log('returned: ' + results.return.toString('hex'));
} catch (err) {
  console.error(err);
}

@dmihal
Copy link
Contributor Author

dmihal commented Feb 9, 2018

Based on the feedback, I made a cleaner implementation in #273

@Silur
Copy link
Contributor

Silur commented Feb 10, 2018

@jwasinger actually I made a Promised (no async dep) version of all etherumjs repos with @wanderer in around last year may

@holgerd77
Copy link
Member

@Silur Tell us more, is this code locked on an USB-Stick in a secret cupboard in a forgotten Lisbon library?? 😄 🔑

@holgerd77
Copy link
Member

Addressed in #546, will close.

holgerd77 added a commit that referenced this issue Mar 11, 2021
…-release

v7.0.1 release / Downgrade bn.js re-export from v5 to v4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants