function creat_user(name) {
name = name?name:null
let d = {name:name}
d.friends = new Set()
return(d)
}
var arya = creat_user(); //arya forget to set name when init
var gendry = creat_user("gendry");
/*
> arya
{ name: null, friends: Set(0) {} }
> gendry
{ name: 'gendry', friends: Set(0) {} }
>
*/
async function get_name(user) {
let name = user.name;
if (name === null) {
//=============>perform similiar to Throw new Error()
//=============>but recoverable
console.log("wait error handling")
let recoverable = perform("null-error"); //just like Raise/Throw Error
name = await recoverable; //[&A]
console.log("error recovered!!")
}
return(name)
}
async function make_friends(user1, user2) {
let f = frame(
get_name, //====>the original function,used as function-template here
err => {
console.log("error type: ",err)
if(err==="null-error"){ //resume the "null-error" with a good-value "arya"
//resume similiar to try {} catch {}
//but is used to recover the excution
resume("arya") //====================>resume the function at [&A]
}
}
);
user1.friends.add(await f(user2));
user2.friends.add(await f(user1));
}
(async ()=> {await make_friends(arya, gendry);})();
/*
> arya
{ name: null, friends: Set(1) { 'gendry' } }
> gendry
{ name: 'gendry', friends: Set(1) { 'arya' } }
>
*/