ybogdanov/node-sync

can't make function synchonize

Opened this issue · 0 comments

I have a function in nodejs, some reason i want it synchonize before do another step. I research a lot but no solution working. Currently i use node-sync:

My function like this:

function downloadImageIcon(url,path,ori,callback){
    var request = require('request');
    //Sync(function() {
        request.get({url: url, encoding: 'binary'}, function (err, response, body) {
            console.log("start load: " + path);
            //Sync(function() {
                fs.writeFile( ori, body, 'binary', function (err) {
                    if (err)
                        console.log(err);
                    else {// if success then we need to convert it
                        console.log("converting : " + path);
                            fs.writeFileSync( path, imagemagick.convert({
                                srcData: fs.readFileSync(ori),
                                width: 1024,
                                height: 1024,
                                resizeStyle: 'aspectfill', // is the default, or 'aspectfit' or 'fill'
                                gravity: 'Center' // optional: position crop area when using 'aspectfill'
                            }));
                        callback();

                    }
                //});
            });
            console.log("end load: " + path);

    });
    //})
}

  1. - download image from server
  2. - store in local
  3. - convert to different resolution

Function work well but i need it to synchronize when called. Thanks for investigate!