yield的理解

yield

带有保存功能的return

  1. 2个yield
    将第一个yield返回一个常量
    1
    2
    3
    4
    const response:RequestResponse = yield(() => {
    return request(action.payload)
    return getMyLocation().then(location => ({location}))
    })()
1
yield effects.put(updateGroupInfoAction(groupInfoData))
  1. yield关键字用来暂停和恢复一个生成器(function* 或遗留的生成器函数)
    [rv] = yield [expression]
    rv:返回传递给生成器的next()方法的可选值,以恢复执行
    expression:通过迭代器协议从生成器函数返回的值
    (1)yield使生成器函数执行暂停
    基于生成器版本的return关键字
    (2)返回实际为IteratorResult对象

    2个属性:
    value:yield表达式求值结果
    done:false(表示未完成)

  2. yield暂停
    用next()方法恢复执行 —> 结束
    (1)yield
    (2)throw异常,生成器完全停止执行
    (3)到达生成器函数结尾,返回undefined,done=true
    (4)return执行结束 done = true

  3. function* countAppleSales() —> appleStore.next()

  4. 函数运行到yield时退出,并保留上下文,下次进入时可以继续进行