Skip to content

Commit

Permalink
Fix Meteor.promisify and add a test for it
Browse files Browse the repository at this point in the history
This fixes a regression introduced in commit 87f8697.
  • Loading branch information
OleksandrChekhovskyi committed Aug 7, 2024
1 parent 22fbca5 commit 35a6df1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/meteor/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ Meteor.promisify = function (fn, context, errorFirst) {
}

return function () {
var self = this;
var filteredArgs = Array.prototype.slice.call(arguments)
.filter(function (i) { return i !== undefined; });

return new Promise(function (resolve, reject) {
var callback = Meteor.bindEnvironment(function (error, result) {
var _error = error, _result = result;
Expand All @@ -100,11 +104,9 @@ Meteor.promisify = function (fn, context, errorFirst) {
resolve(_result);
});

var filteredArgs = Array.prototype.slice.call(arguments)
.filter(function (i) { return i !== undefined; });
filteredArgs.push(callback);

return fn.apply(context || this, filteredArgs);
return fn.apply(context || self, filteredArgs);
});
};
};
Expand Down
21 changes: 21 additions & 0 deletions packages/meteor/helpers_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,24 @@ Tinytest.add("environment - startup", function (test) {
});
test.isTrue(called);
});

Tinytest.addAsync("environment - promisify", async function (test) {
function TestClass(value) {
this.value = value;
}

TestClass.prototype.method = function (arg1, arg2, callback) {
var value = this.value;
setTimeout(function () {
callback(null, arg1 + arg2 + value);
}, 0);
};

TestClass.prototype.methodAsync = Meteor.promisify(TestClass.prototype.method);

var instance = new TestClass(5);
test.equal(await instance.methodAsync(1, 2), 8);

let asyncMethodWithContext = Meteor.promisify(instance.method, instance);
test.equal(await asyncMethodWithContext(2, 3), 10);
});

0 comments on commit 35a6df1

Please sign in to comment.