基于Lodash
const a = [1, 2, 3];
const b = [...a, 4, 5];const a = [1, 2, 3];
const b = _.filter(a, v => v !== 2);const a = [1, 2, 3];
const b = _.map(a, v => v === 2 ? 4 : v)const a = [{
name: 'Tom',
friends: [{ name: 'Jerry' }],
age: 2
}, {
name: 'Robert',
friends: [
{ name: 'Tom' },
{ name: 'Jerry' }
],
age: 4
}];
// TODO: 为Tom添加一个到Robert的朋友Test.create({name: 't1'}, function(t1) {
Test.create({name: 't2'}, function(t2) {
Test.create({name: 't3'}, function(t3) {
Test2.create({name: 't11', testId: t1.id},
output);
});
});
});
Test.create({name: 't1'})
.then(function(t1) {
Test.create({name: 't2'})
.then(function (t2) {
Test.create({name: 't3'})
.then(function(t3) {
Test2.create(
{
name: 't11',
testId: t1.id
})
.then(output);
});
});
});
async function parallelMap(urls: Array<string>) {
return Promise.all(_.map(
urls,
async (url) => {
const res = await fetch(url);
if (!res.ok) {
throw new Error('fetch failed.');
}
const result = await res.json();
return result;
}
));
}async function seriallyMap(urls: Array<string>) {
const results = [];
await _.reduce(
urls,
async (pms, url) => {
await pms;
const res = await fetch(url);
if (!res.ok) {
throw new Error('fetch failed.');
}
const result = await res.json();
results.push(result);
},
Promise.resolve()
);
return results;
}