Skip to content

Commit

Permalink
fix: process must works with async
Browse files Browse the repository at this point in the history
  • Loading branch information
rubeniskov committed Dec 7, 2020
1 parent db55765 commit e42f37d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,13 @@ const mutantJson = (target, process, opts) => {
const processMutation = (value, path, target) => {
let ret;

process((patch) => {
const presult = process((patch) => {
ret = applyPatches(patch, path, target);
}, value, path, target);

if (isPromise(presult)) {
return presult.then(() => ret);
}
return ret;
};

Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,28 @@ test('should mutate using a promise', async (t) => {
t.deepEqual(actual, expected);
});

test('should mutate using async', async (t) => {

const flattenObjectPromises = {
foo: Promise.resolve(1),
bar: Promise.resolve(2),
};

const expected = {
foo: 100,
bar: 200,
};

const actual = await mutateJson(Promise.resolve(flattenObjectPromises), async (mutate, value) => {
const op = await Promise.resolve({
value: value * 100,
});
mutate(op);
});

t.deepEqual(actual, expected);
});


test('should force return a promise even when no promise detected', async (t) => {

Expand Down

0 comments on commit e42f37d

Please sign in to comment.