mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Merge branch '251-render-promise' of https://github.com/austinkelleher/marko
This commit is contained in:
commit
f4bc55ec5e
@ -71,6 +71,29 @@ module.exports = {
|
||||
return this;
|
||||
},
|
||||
|
||||
then: function(fn, fnErr) {
|
||||
var self = this;
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
self.on('error', reject);
|
||||
self.on('finish', function(data) {
|
||||
try {
|
||||
resolve(fn(data));
|
||||
} catch(err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (fnErr) {
|
||||
promise = promise.catch(fnErr);
|
||||
}
|
||||
return promise;
|
||||
},
|
||||
|
||||
catch: function(fnErr) {
|
||||
return this.then(undefined, fnErr);
|
||||
},
|
||||
|
||||
appendTo: function(referenceEl) {
|
||||
var newNode = this.getNode(referenceEl.ownerDocument);
|
||||
dom.appendTo(newNode, referenceEl);
|
||||
|
||||
@ -223,7 +223,7 @@ var proto = AsyncStream.prototype = {
|
||||
if (state.writer.end) {
|
||||
state.writer.end();
|
||||
} else {
|
||||
state.events.emit('finish');
|
||||
state.events.emit('finish', this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -292,7 +292,7 @@ var proto = AsyncStream.prototype = {
|
||||
var state = this._state;
|
||||
|
||||
if (event === 'finish' && state.finished) {
|
||||
callback();
|
||||
callback(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -304,7 +304,7 @@ var proto = AsyncStream.prototype = {
|
||||
var state = this._state;
|
||||
|
||||
if (event === 'finish' && state.finished) {
|
||||
callback();
|
||||
callback(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@ -57,6 +57,19 @@ describe('async-writer' , function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should resolve promise upon finish', function() {
|
||||
var out = new AsyncStream();
|
||||
|
||||
out.write('1');
|
||||
out.write('2');
|
||||
|
||||
return out.end().then((data) => {
|
||||
const output = out.getOutput();
|
||||
expect(output).to.equal('12');
|
||||
expect(data.getOutput()).to.equal('12');
|
||||
});
|
||||
});
|
||||
|
||||
it('should render a series of sync and async calls correctly', function(done) {
|
||||
var out = new AsyncStream();
|
||||
out.write('1');
|
||||
@ -250,6 +263,46 @@ describe('async-writer' , function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should catch error in promise catch', function(done) {
|
||||
const out = new AsyncStream();
|
||||
|
||||
let errors = [];
|
||||
|
||||
out.on('error', function(e) {
|
||||
errors.push(e);
|
||||
});
|
||||
|
||||
out.write('1');
|
||||
|
||||
let asyncOut = out.beginAsync();
|
||||
setTimeout(function() {
|
||||
asyncOut.error(new Error('test'));
|
||||
}, 10);
|
||||
|
||||
out.write('3');
|
||||
out.end().catch((err) => {
|
||||
expect(errors.length).to.equal(1);
|
||||
expect(err).to.be.an('error');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should catch error in promise catch if `error` listener only set inside mixin', function(done) {
|
||||
const out = new AsyncStream();
|
||||
|
||||
out.catch((err) => {
|
||||
expect(err).to.be.an('error');
|
||||
expect(out.getOutput()).to.equal('1');
|
||||
done();
|
||||
}).then((data) => {
|
||||
throw new Error('Should not get here!');
|
||||
});
|
||||
|
||||
out.write('1');
|
||||
out.error(new Error('test'));
|
||||
out.write('2');
|
||||
});
|
||||
|
||||
it('should support chaining', function(done) {
|
||||
var errors = [];
|
||||
var out = new AsyncStream()
|
||||
|
||||
@ -41,6 +41,15 @@ it('async', function(done) {
|
||||
});
|
||||
});
|
||||
|
||||
it('promise', function(done) {
|
||||
const out = new AsyncVDOMBuilder();
|
||||
out.element('div', {}, 0);
|
||||
out.end().then((tree) => {
|
||||
expect(tree.childNodes.length).to.equal(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('async flush', function(done) {
|
||||
var out = new AsyncVDOMBuilder();
|
||||
out.on('update', function(tree) {
|
||||
|
||||
1
test/autotests/api/load-render-promise/template.marko
Normal file
1
test/autotests/api/load-render-promise/template.marko
Normal file
@ -0,0 +1 @@
|
||||
- Hello ${data.name}!
|
||||
16
test/autotests/api/load-render-promise/test.js
Normal file
16
test/autotests/api/load-render-promise/test.js
Normal file
@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const nodePath = require('path');
|
||||
|
||||
exports.check = function(marko, markoCompiler, expect, done) {
|
||||
let template = marko.load(nodePath.join(__dirname, 'template.marko'));
|
||||
|
||||
template.render({
|
||||
name: 'John'
|
||||
}).then((out) => {
|
||||
expect(out.getOutput()).to.equal('Hello John!');
|
||||
done();
|
||||
}).catch((err) => {
|
||||
done(err);
|
||||
});
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user