JS 中 return 定义数据延迟 1 秒的函数
Function in JS that return defined data with 1 second delay
所以,我有一个任务,做一个函数'mocker',它将return定义数据用1秒delay.The问题是我我是 JavaScript 世界的新手,不知道该怎么做 this.I 尝试采用这种方式:
mocker = function mocker(data) {
var delayInMilliseconds = 1000;
setTimeout(function () {
return data;
}, delayInMilliseconds);};
但不满足作业要求。
我有这个例子:
const getUsers = mocker([{id: 1, name: 'User1'}]);
getUsers().then((users) => { // Will fire after 1 second.
console.log(users); // result: [{id: 1, name: 'User1'}];
});
这是测试的函数描述:
describe('mocker', () => {
describe('users mocker', () => {
const usersData = [{id: 1, login: 'mickey'}, {id: 2, login: 'billy77'}, {id: 3, login: 'coooool123'}];
let getUsers;
beforeEach(() => {
getUsers = mocker(usersData);
});
it('should return users data', async () => {
const resultData = await getUsers();
assert.deepStrictEqual(resultData, usersData);
});
it('should return users data in asynchronous way', () => {
const resultData = getUsers();
assert.notDeepStrictEqual(resultData, usersData);
});
});
});
@param data: {Array | Object}
@returns {Function}
你们能帮帮我吗?提前致谢。
测试表明它比仅仅创建一个 return 秒后发送一些数据的函数要复杂一些。
这一行:
getUsers = mocker(usersData);
是说 mocker
需要 1) 接受一些数据和 2) return 我们分配给 getUsers
的函数。当我们调用 getUsers
时,它将调用该函数以 return 数据的承诺。
所以当我们到达这里时:
const resultData = await getUsers();
我们正在调用我们分配给 getUsers
的函数,该函数承诺 return 一些数据(或不)一秒钟后。
const usersData = [{id: 1, login: 'mickey'}, {id: 2, login: 'billy77'}, {id: 3, login: 'coooool123'}];
// `mocker` accepts some data and returns a
// function. This function is assigned to `getData`, and
// when `getData` is called this is the function that gets executed.
// It returns a promise that after one second data will be returned.
function mocker(usersData) {
return function () {
return new Promise((res, rej) => {
setTimeout(() => res(usersData), 1000);
});
}
}
// So we call `mocker` and assign the function it
// returns to `getUsers`
const getUsers = mocker(usersData);
async function main() {
// We can then call `getUsers` to get the data
// after one second has passed
console.log(await getUsers());
}
main();
其他文档
所以,我有一个任务,做一个函数'mocker',它将return定义数据用1秒delay.The问题是我我是 JavaScript 世界的新手,不知道该怎么做 this.I 尝试采用这种方式:
mocker = function mocker(data) {
var delayInMilliseconds = 1000;
setTimeout(function () {
return data;
}, delayInMilliseconds);};
但不满足作业要求。 我有这个例子:
const getUsers = mocker([{id: 1, name: 'User1'}]);
getUsers().then((users) => { // Will fire after 1 second.
console.log(users); // result: [{id: 1, name: 'User1'}];
});
这是测试的函数描述:
describe('mocker', () => {
describe('users mocker', () => {
const usersData = [{id: 1, login: 'mickey'}, {id: 2, login: 'billy77'}, {id: 3, login: 'coooool123'}];
let getUsers;
beforeEach(() => {
getUsers = mocker(usersData);
});
it('should return users data', async () => {
const resultData = await getUsers();
assert.deepStrictEqual(resultData, usersData);
});
it('should return users data in asynchronous way', () => {
const resultData = getUsers();
assert.notDeepStrictEqual(resultData, usersData);
});
});
});
@param data: {Array | Object}
@returns {Function}
你们能帮帮我吗?提前致谢。
测试表明它比仅仅创建一个 return 秒后发送一些数据的函数要复杂一些。
这一行:
getUsers = mocker(usersData);
是说 mocker
需要 1) 接受一些数据和 2) return 我们分配给 getUsers
的函数。当我们调用 getUsers
时,它将调用该函数以 return 数据的承诺。
所以当我们到达这里时:
const resultData = await getUsers();
我们正在调用我们分配给 getUsers
的函数,该函数承诺 return 一些数据(或不)一秒钟后。
const usersData = [{id: 1, login: 'mickey'}, {id: 2, login: 'billy77'}, {id: 3, login: 'coooool123'}];
// `mocker` accepts some data and returns a
// function. This function is assigned to `getData`, and
// when `getData` is called this is the function that gets executed.
// It returns a promise that after one second data will be returned.
function mocker(usersData) {
return function () {
return new Promise((res, rej) => {
setTimeout(() => res(usersData), 1000);
});
}
}
// So we call `mocker` and assign the function it
// returns to `getUsers`
const getUsers = mocker(usersData);
async function main() {
// We can then call `getUsers` to get the data
// after one second has passed
console.log(await getUsers());
}
main();
其他文档