{
var options = {
host: url.parse(fileUrl).host,
port: 80,
path: url.parse(fileUrl).pathname
};
var self=this;
var file_name = decodeURI(url.parse(fileUrl).pathname.split('/').pop());
var filePath=this.downloadDir + file_name;
var file = fs.createWriteStream(filePath);
var Ticker=function(){};
util.inherits(Ticker,Et);
var ticker = new Ticker();
var loaded = new Promise(function(resolve, reject) {ticker.on("tick", resolve);});
//catch 捕獲回調的第一個參數,通常情況下是err參數,這裏是res
phttp.getAsync(options).catch(function(res)
{
res.on('data', function(data) {
file.write(data);
});
res.on('end', function(){
file.end();
//cb(filePath);
console.log(file_name + ' downloaded to ' + self.downloadDir);
ticker.emit('tick');
});
});
//這裏實現對異步函數同步邏輯
yield loaded.then(function(){console.log('event tick');
});
return filePath;
});