3
我這個代碼測試phantom-node
:Phantom.js - 如何使用promise而不是回調?
var http = require('http');
http.createServer(function (req, res) {
res.write('<html><head></head><body>');
res.write('<p>Write your HTML content here</p>');
res.end('</body></html>');
}).listen(1337);
var phantomProxy = require('phantom-proxy');
phantomProxy.create({'debug': true}, function (proxy) {
proxy.page.open('http://localhost:1337', function (result) {
proxy.page.render('scratch.png', function (result) {
proxy.end(function() {
console.log('done');
});
}, 1000);
});
});
它的工作原理,但我想改變它的東西,如:
phantomProxy.create()
.then(something)=>{...}
.then(something)=>{...}
.then(something)=>{...}
.catch((err)=>{
console.log(err);
proxy.end();
}
,使其更易於閱讀。任何建議?
您可能希望有看看「異步」模塊,我記得內置在node.js中。它允許你做一些類似於你想要的東西。 –
可能有一個庫可以爲你做,但它很容易創建你自己的承諾幷包裝所有這些功能。你想使用像ES6承諾還是像藍鳥這樣的圖書館? – Spidy
我認爲不是ES6。但我絕對想要最簡單的事情開始。 – sooon