Код возвращает то что нужно, но проверку не проходит.
db.js
import { loadData } from './storage.js';
export const getUser = (userId) => new Promise((resolve, reject) => {
//check userId and reject if it's missing
if (!userId) {
reject (new Error ('No User Id!'));
}
setTimeout(() => {
//use loadData and resolve with the user object if the id is present
resolve (loadData(userId))
}, 200);
});
storage.js
const userData = { users: [
{
id: 1,
name: 'Jack',
friends: [ 23, 125 ],
}, {
id: 23,
name: 'Jane',
friends: [ 125 ],
}, {
id: 125,
name: 'Jill',
friends: [ 1 ],
}
]
}
export const loadData = (userId) => {
for (let i=0;i<userData.users.length;i++){
if (userData.users[i].id===userId)
return userData.users [i];
}
}