const videoA = { id: 'a', title: 'Create a GraphQL schema', duration: 120, watched: true }; const videoB = { id: 'b', title: 'Ember.js CLI', duration: 240, watched: false }; const videos = [videoA, videoB]; const createVideo = ({ title, duration, released }) => { const video = { id: (new Buffer(title, 'utf8')).toString('base64'), title, duration, released }; videos.push(video); return video; }; const getVideoById = (id) => new Promise((resolve) => { const [video] = videos.filter(currentVideo => currentVideo.id === id); resolve(video); }); // Shortcut: we have the data immediately, so no promise. // const getVideos = () => videos; // General case: asynchronously load the videos const getVideos = () => new Promise(resolve => resolve(videos)); const getObjectById = (type, id) => { const types = { video: getVideoById }; return types[type](id); }; exports.createVideo = createVideo; exports.getObjectById = getObjectById; exports.getVideoById = getVideoById; exports.getVideos = getVideos;