Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Deployment scripts with async/await #501

Closed
mezrin opened this issue Jul 17, 2017 · 13 comments
Closed

Deployment scripts with async/await #501

mezrin opened this issue Jul 17, 2017 · 13 comments

Comments

@mezrin
Copy link

mezrin commented Jul 17, 2017

It is possible to configure test scripts to work with async/await. (JavaScript test scripts, of course).

It would be amazing to have the same for deployment scripts. Because all that .then really destroy readability of deployment scripts.

@travs
Copy link

travs commented Jul 20, 2017

I have my tests working with async/await right now, but I do have difficulty using async/await syntax in my migrations scripts for some reason.
It seems like lines after the first await call get skipped sometimes.
Not sure if this is just my setup, or if anyone else experience the same thing.

Using Node v8.1.3.

@brynbellomy
Copy link

Happens to me as well, Node 8.1.3, Truffle 3.4.8.

@felixwatts
Copy link

+1

@elenadimitrova
Copy link

Same here, I had symptoms of #386 when trying to use async/await in migrations

@tcoulter
Copy link
Contributor

tcoulter commented Sep 27, 2017

When writing migrations, remember that your migration function stages deployment tasks -- it's not intended to run those tasks at execution time of the migration function. This behavior and Promise structure of migrations was built in a time when async/await was less prevalent.

We plan on making migrations work with async/await in the future. However, in the meantime, if you'd like to use async/await in your migrations, follow this pattern:

2_some_migration.js

var SomeContract = artifacts.require("SomeContract");

// An async function
const someAsyncFunction = async function someAsyncFunction() {
  // ...
};

// Your actual migration
module.exports = (deployer) => {
  deployer.deploy(SomeContract).then(() => await someAsyncFunction())

  // Alternatively, just start a chain without a deployment
  deployer.then(() => await someAsyncFunction())
}

This will ensure your async functions are executed at the right time, and those steps are added to the Promise chain.

Again, we will be revamping this in the future. Would love to hear your thoughts on how migrations could be made easier.

@tcoulter
Copy link
Contributor

Edit: I wrote that code on the fly; didn't test it. Please correct me if there are any typos!

@rodiazet
Copy link

rodiazet commented Mar 8, 2018

I'm trying to use this workaround but it seems that it doesn't have change to work. It's not possible to wrap call with await inside non-async function. So () => await someAsyncFunction() won't work or I'm missing something important?

@shkfnly
Copy link

shkfnly commented Mar 17, 2018

@rodiazet what he meant was

  var SomeContract = artifacts . require ( " SomeContract " );

 // An async function
 const someAsyncFunction = async function someAsyncFunction () {
   // ...
 };

 // Your actual migration
 module . exports = ( deployer ) => {
   deployer . deploy (SomeContract). then (() => await someAsyncFunction ())

   // alternative, just start a chain without a deployment
   deployer . then (async() => await someAsyncFunction ())
 } 

I wrapped all this together like

module.exports = function (deployer) {
  deployer.then(async () => {
    await deployer.deploy(Division)
    await deployer.link(Division, [DistributeToken, ProjectLibrary, ReputationRegistry, TokenRegistry, Project])
    await deployer.deploy(ProjectLibrary)
    await deployer.link(ProjectLibrary, [TokenRegistry, ReputationRegistry, ProjectRegistry])
    await deployer.deploy(TokenRegistry)
    await deployer.deploy(ReputationRegistry)
    await deployer.deploy(ProjectRegistry)
    await deployer.deploy(DistributeToken, TokenRegistry.address, ReputationRegistry.address)
    await deployer.deploy(PLCRVoting, TokenRegistry.address, ReputationRegistry.address, ProjectRegistry.address)

    let PRInstance = await ProjectRegistry.deployed()
    let TRInstance = await TokenRegistry.deployed()
    let RRInstance = await ReputationRegistry.deployed()
    return Promise.all([
      PRInstance.init(DistributeToken.address, TokenRegistry.address, ReputationRegistry.address, PLCRVoting.address),
      TRInstance.init(DistributeToken.address, ProjectRegistry.address, PLCRVoting.address),
      RRInstance.init(DistributeToken.address, ProjectRegistry.address, PLCRVoting.address)
    ])
  })
}

Hope this helps!

Shout out to http://y-nakajo.hatenablog.com/entry/2018/01/15/174743
and https://ethereum.stackexchange.com/questions/30572/truffle-post-deployment-actions
for the insight

@kigawas
Copy link

kigawas commented May 17, 2018

@shkfnly Thanks for sharing 👍

@code-brewer
Copy link

code-brewer commented Nov 1, 2018

It seems like lines after the first await call get skipped sometimes.

Yes, i encountered this issue with node v8.10.0, it look like await() don't return but the test flow exited normally. Finally i upgrade nodejs to 9.11.1 and then run smoothly

bingen added a commit to aragon/dao-templates that referenced this issue Dec 28, 2018
@stale
Copy link

stale bot commented Dec 31, 2018

Thank you for raising this issue! It has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. If you would like to keep this issue open, please respond with information about the current state of this problem.

@stale stale bot added the stale label Dec 31, 2018
@stale
Copy link

stale bot commented Jan 7, 2019

There has been no new activity on this issue since it was marked as stale 7 days ago, so it is being automatically closed. If you'd like help with this or a different problem, please open a new issue. Thanks!

@saeedjaafari
Copy link

hi dear
i gave this problem :

truffle(develop)> var poe = await ProofOfExistence1.at(ProofOfExistence1.address) Uncaught: Error: ProofOfExistence1 has no network configuration for its current network id (5777). at evalmachine.<anonymous>:1:48 at Function.getter (c:\Users\Apple\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\contract\constructorMethods.js:282:1) at Function.get (c:\Users\Apple\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\contract\properties.js:129:1) at Function.getter (c:\Users\Apple\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\contract\constructorMethods.js:285:1) at Function.network (c:\Users\Apple\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\contract\properties.js:108:1)

how can i solved it??
thx

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

No branches or pull requests