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

[Question] Waiting for Pull to be finished #357

Closed
Skarlso opened this issue Mar 31, 2017 · 1 comment
Closed

[Question] Waiting for Pull to be finished #357

Skarlso opened this issue Mar 31, 2017 · 1 comment

Comments

@Skarlso
Copy link

Skarlso commented Mar 31, 2017

Hi!

Sorry, this is going to be a noob question. I'm just learning these things, and want to put together a node module which works with docker.

And this module is awesome to use as far as I understand Promises right now, and structure of exporting object prototype functions.

So I have a DockerHelper object with the exported function pullImage which should handle, pulling an image. And than, I'm calling that function and once it finishes, I want to create a container out of that pulled image.

Now, I tried doing it with bluebird, or promises, doing the container create in the then, but it immediately jumps to that section ( which, as far as I understand, is an intended behaviour of asynchronous functions ).

So.. I kind of want to wait for that pull to finish. I found the helper followProgress which is sort of what I want. Especially the onFinish part. But that's in my function which handles the pull. And I'm creating the container in another function. Separation of concern and all that.

Thus my question is, how do people do this? :D

Here is all my code as messy as it is right now: https://github.com/Skarlso/miner
This line in particular: https://github.com/Skarlso/miner/blob/master/docker.js#L23

I also tried it with new Promise(function(resolve, reject... and than return the promise and do the then part like this:

var pull = dHelper.pullMinecraftImageVersion(version);
pull.then(function(result) {
    console.log('Done pulling.');
    console.log('Create container out of the pulled image with name: %s', chalk.green(config.containerName));
}).catch(function(err) {
    console.log(err);
});

If I understood Promises, the then was supposed to wait? :D Maybe I misunderstood the concept described here: https://www.promisejs.org/. If I understood things correctly, promises are async by nature so you aren't supposed to wait on them. So I'm stuck with the progress helper on waiting for the pull to finish? Or is my entire design flawed here?

Thanks for the all the help folks!!! And sorry for the noobness. :)

@Skarlso Skarlso closed this as completed Apr 17, 2017
@Skarlso
Copy link
Author

Skarlso commented Apr 17, 2017

I solved it by having the action taken onFinish with the helper like this:

    docker.pull(config.repoTag + version, (err, stream) => {
        docker.modem.followProgress(stream, onFinished, onProgress);

        function onFinished(err, output) {
            spinner.stop(false);
            if (!err) {
                console.log('\nDone pulling.');
                ver.saveServerVersion(name, version);
            } else {
                console.log(err);
                process.exit(1);
            }
        }
        function onProgress(event) {
        }
    });

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

No branches or pull requests

1 participant