|
// retoor <retoor@molodetz.nl>
|
|
|
|
import "scheduler" for Scheduler, Future
|
|
import "timer" for Timer
|
|
|
|
class MockApi {
|
|
static fetchUser { async { |id|
|
|
Timer.sleep(1)
|
|
return {"id": id, "name": "User%(id)", "active": true}
|
|
}}
|
|
|
|
static fetchPosts { async { |userId|
|
|
Timer.sleep(1)
|
|
return [
|
|
{"id": 1, "userId": userId, "title": "Post 1"},
|
|
{"id": 2, "userId": userId, "title": "Post 2"}
|
|
]
|
|
}}
|
|
|
|
static createPost { async { |userId, title|
|
|
Timer.sleep(1)
|
|
return {"id": 3, "userId": userId, "title": title, "created": true}
|
|
}}
|
|
|
|
static getUserWithPosts(userId) {
|
|
var fetchUser = MockApi.fetchUser
|
|
var fetchPosts = MockApi.fetchPosts
|
|
var user = await fetchUser(userId)
|
|
var posts = await fetchPosts(userId)
|
|
|
|
user["posts"] = posts
|
|
return user
|
|
}
|
|
}
|
|
|
|
var fetchUser = MockApi.fetchUser
|
|
var user = await fetchUser(42)
|
|
System.print(user["name"]) // expect: User42
|
|
|
|
var fetchPosts = MockApi.fetchPosts
|
|
var posts = await fetchPosts(42)
|
|
System.print(posts.count) // expect: 2
|
|
|
|
var createPost = MockApi.createPost
|
|
var newPost = await createPost(42, "New Post")
|
|
System.print(newPost["created"]) // expect: true
|
|
|
|
var fullUser = MockApi.getUserWithPosts(99)
|
|
System.print(fullUser["name"]) // expect: User99
|
|
System.print(fullUser["posts"].count) // expect: 2
|