Nuxt Instance Unavailable - Async Code in Vue and Nuxt with the Composition API (+ raffle 🎁)

Alexander Lichter
19 Apr 202418:14

Summary

TLDR在这段视频脚本中,主讲人讨论了一个常见的编程错误信息:'next instance unavailable',这通常与异步代码有关。视频首先提到了一个延长的抽奖活动,鼓励观众参与评论以赢取前往vew conf的免费门票。随后,主讲人通过一个演示应用程序,展示了如何引发和解决这个错误信息。他们解释了为什么这个错误会发生,以及如何在Vue 3和Knexus(一种状态管理库)中处理它。主讲人提供了多种解决方案,包括不使用异步权重、重新排列代码顺序、使用`call with KN`和`app.run with context`,以及启用实验性的`as context`选项。视频最后鼓励观众分享他们遇到这个错误时的解决方法,并提供了相关资源链接。

Takeaways

  • 🌐 视频讨论了一个常见的错误消息 'next instance unavailable',通常与异步代码有关。
  • 🎁 主持人宣布了一个抽奖活动,延长了一周,并邀请观众在评论区分享他们想参加 Vew Conf 的原因以及他们使用 Vew 或 Next.js 构建的项目。
  • 🔍 主持人展示了如何触发 'next instance unavailable' 错误,并解释了它的含义以及如何修复。
  • 🛠️ 为了演示错误,主持人创建了一个简单的应用程序,并故意引入了异步代码来触发错误。
  • 📚 讨论了如何通过移动代码到不同的组件或使用不同的生命周期钩子来解决错误。
  • 🔄 主持人提到了 Vue 团队成员 Anthony Fu 的博客文章,该文章解释了 Vue 中异步 composable 的工作原理。
  • 👨‍🏫 主持人解释了为什么在 script setup 中使用异步代码时会出现问题,以及 Vue 是如何处理组件实例的。
  • 🔧 提供了几种解决方案,包括重新排序代码、使用不同的生命周期钩子、使用 `runWithContext` 和 `callWith` 方法,以及启用实验性的 `useAsyncContext` 选项。
  • 🔗 提到了 `useAsyncContext` 是一个实验性功能,它允许在所有嵌套的异步 composable 中使用原生的异步上下文。
  • 🚀 最后,主持人鼓励观众在评论中分享他们如何处理这个错误,以及他们打算尝试哪些解决方案。

Q & A

  • 什么是'next instance unavailable'错误信息,它通常在什么情况下出现?

    -这个错误信息通常与异步代码有关,当Vue中的某个composable需要访问Vue实例的上下文,但是这个上下文不可用时,就会出现这个错误。这通常发生在使用script setup时,尤其是在异步函数和composable混合使用的情况下。

  • 视频提到的'raffle'是什么,以及如何参与?

    -视频中提到的'raffle'是一个抽奖活动,参与者需要在视频下方留言,说明为什么想去Vew Conf以及他们已经使用Vue构建了什么。然后,视频作者将在下周抽取一名获奖者,赠送免费门票。

  • 视频作者提到了哪些方法来解决'next instance unavailable'错误?

    -视频作者提到了多种解决方法,包括:避免在composables中使用异步代码,重新排列代码顺序使得composables在异步函数之前调用,使用`callWithKN`和`app.runWithContext`,以及启用Vue的实验性特性`useAsyncContext`。

  • 如何使用`useAsyncContext`来解决'next instance unavailable'错误?

    -在Vue应用中,可以通过在`createApp`函数中设置`useAsyncContext`为`true`来启用这个实验性特性。这将允许异步composables访问Vue实例的上下文,从而解决错误。

  • 视频作者提到了`script setup`的局限性,具体是什么?

    -`script setup`在顶层使用时可以很好地工作,因为它允许编译器执行一些特殊的处理来保持Vue实例的上下文。但是,当在`script setup`之外或者多层嵌套的composables中使用时,这种处理就不再有效,可能会导致`next instance unavailable`错误。

  • 为什么`useAsyncContext`是一个实验性特性,它有什么潜在的问题?

    -`useAsyncContext`是基于Node.js的`asyncLocalStorage`实现的,这意味着它目前只在Node.js环境中有支持。由于Vue和Nitro等框架尝试保持运行时的中立性,这个特性在其他运行时环境中可能无法工作,因此它被标记为实验性。

  • 视频作者提到了TC39的提案,这个提案的目的是什么?

    -TC39的提案旨在将`async context`作为一个语言原生特性引入到JavaScript中。这样,无论在哪个运行时环境中,只要支持JavaScript,`async context`就能正常工作,不再依赖于特定的平台如Node.js或Deno。

  • 如何检查`useAsyncContext`是否在自己的项目中有效?

    -可以通过在项目的CI/CD流程中启用`useAsyncContext`并进行测试来检查它是否有效。如果项目托管在像Cloudflare Workers这样的边缘或无服务器运行时上,并且使用Node.js,那么启用`useAsyncContext`可能会有效。

  • 视频作者提到了`useKNApp`和`app.runWithContext`,这些API是如何帮助解决错误的?

    -`useKNApp`可以用来获取Vue应用的实例,而`app.runWithContext`允许在特定的上下文中执行函数。通过将需要Vue实例上下文的composables包装在`app.runWithContext`中,可以确保这些composables不会丢失上下文,从而解决错误。

  • 视频作者建议的测试策略是什么?

    -视频作者建议使用CI/CD流程中的测试来捕捉像`next instance unavailable`这样的错误。如果没有CI/CD,作者还提到了一个关于在Vue中进行测试的视频,可以作为参考。

Outlines

00:00

😀 解决 'next instance unavailable' 错误

本段视频脚本介绍了一个常见的错误消息 'next instance unavailable',解释了它的含义,并提供了解决此问题的方法。视频开始时,主持人提到了上一个视频中的抽奖活动,由于Google和YouTube的一些问题,决定将抽奖延长一周。随后,主持人介绍了错误消息通常与异步代码有关,并展示了如何在一个示例应用程序中触发和修复此错误。通过将异步代码移动到一个可复用的composable中,展示了错误是如何发生的,并提出了一些解决方案,包括检查文档和理解错误的根本原因。

05:02

🔍 深入理解异步代码和组件实例

在第二段中,视频脚本深入探讨了为什么异步代码会导致 'next instance unavailable' 错误。解释了Vue组件实例如何在全局变量中存储,以及异步函数如何与此交互。提到了设计限制和composable的使用建议,即默认情况下不应使用异步。还讨论了Vue如何处理异步composable和代码,以及如何通过编译时魔术解决 'script setup' 中的问题。最后,提出了一些解决方案,包括重新排列代码顺序,使用 'app.runWithContext' 和 'callWithKN' 等方法。

10:03

🛠️ 使用不同的方法解决异步问题

第三段脚本继续讨论解决异步问题的不同方法。介绍了 'runWithContext' 和 'callWithKN' 这两种方法,它们可以帮助保持上下文不丢失,并且不需要显式传递给composable。此外,还提到了一个新的实验性功能 'asContext',它可以在不更改任何代码的情况下解决问题。解释了 'asContext' 是如何工作的,以及为什么它是实验性的。还提到了tc39的提案,该提案旨在将 'asContext' 作为JavaScript语言的一个原生特性引入。

15:04

🎉 总结和观众互动

在视频的最后部分,主持人总结了关于 'next instance unavailable' 错误的信息,包括如何触发此错误、如何预防以及几种不同的解决方法。提到了不立即使用异步权重、重新排列代码顺序、使用 'callWithKN' 和 'app.runWithContext',以及实验性的 'asContext' 选项。主持人鼓励观众分享他们是否遇到过这个错误,以及他们是如何解决问题的。此外,还提到了与Michael讨论的关于View和Teleports的最新D of View剧集,并邀请观众提出问题、建议和反馈。

Mindmap

Keywords

💡Arrow message

Arrow message 是指在开发过程中遇到的一个错误提示,它通常与异步代码有关。在视频中,这个错误提示是 'next instance unavailable',意味着在异步函数中无法访问到 Vue 的实例。这个概念是视频的核心,因为整个视频都在解释这个错误的含义以及如何解决它。

💡asynchronous code

异步代码是指在程序执行过程中,某些操作可以并行进行,而不需要等待前一个操作完成。在视频中,异步代码的使用导致了 'next instance unavailable' 的错误,因为异步操作在执行时,Vue 组件的实例可能还未完全创建好。

💡composable

Composable 是 Vue 3 中引入的一个概念,它是一种可复用的逻辑单元,可以在组件之间共享。在视频中,composable 被用于演示如何触发 'next instance unavailable' 错误,并且是解决问题的关键部分。

💡script setup

Script setup 是 Vue 3 中的一个语法糖,它允许开发者以一种更简洁的方式编写组件的脚本逻辑。在视频中,script setup 被用来展示如何在顶层组件中避免 'next instance unavailable' 错误。

💡useAsyncData

useAsyncData 是一个自定义的 composable,用于处理异步数据。在视频中,它被用来演示如何触发 'next instance unavailable' 错误,并且是解决问题的一个关键点。

💡Vue

Vue 是一个用于构建用户界面的渐进式框架,它被广泛应用于前端开发中。在视频中,Vue 的实例和组件是讨论 'next instance unavailable' 错误的上下文。

💡error message

错误消息是在程序执行过程中出现问题时,系统提供的提示信息。在视频中,'next instance unavailable' 是一个特定的错误消息,视频的目的是解释这个错误消息的含义以及如何修复它。

💡component instance

组件实例是指在 Vue 中,当定义了一个组件后,每次使用该组件时创建的具体对象。在视频中,组件实例的不可用是导致 'next instance unavailable' 错误的根本原因。

💡useHead

useHead 是一个 composable,用于管理页面的头部信息,如标题。在视频中,useHead 被用来演示如何在异步函数中安全地使用 composable,以避免错误。

💡context

上下文是指在程序执行过程中,代码运行的环境信息。在视频中,上下文丢失是导致 'next instance unavailable' 错误的原因之一,而解决方案之一是使用 runWithContext 或 callWithKN 来保持上下文。

💡experimental feature

实验性特性是指那些尚未完全稳定,可能在未来版本中有所改变的特性。在视频中,提到的 'as context' 是 Vue 3 的一个实验性特性,它允许在异步代码中保持组件实例的上下文,从而解决 'next instance unavailable' 错误。

Highlights

介绍了常见的错误消息 'next instance unavailable' 及其含义。

解释了如何通过不同的方法来修复 'next instance unavailable' 错误。

提到了关于 'next instance unavailable' 错误的详细文档链接。

讨论了在 Vue 3 中使用 Composition API 时遇到的异步代码问题。

展示了如何在 Vue 应用中重现 'next instance unavailable' 错误。

解释了 Vue 3 中组件实例和异步函数之间的关联问题。

提到了使用 script setup 时编译器如何处理异步代码。

讨论了在 Vue 中使用异步代码时的设计限制。

提出了避免在 compose 中使用异步代码的建议。

提供了解决 'next instance unavailable' 错误的几种不同方法。

介绍了 'run with context' 和 'call with kn' 作为解决问题的方法。

讨论了使用 'useKNApp' 和 'as context' 实验性功能来解决错误。

解释了 'as context' 是如何工作的以及它为何是实验性功能。

提到了 TC39 提议将 'as context' 作为 JavaScript 的语言原语。

讨论了 'as context' 在不同运行时中的支持情况。

鼓励观众尝试 'as context' 并检查其在 CI/CD 中的效果。

提供了一个总结,概述了 'next instance unavailable' 错误的相关信息和解决方法。

Transcripts

play00:00

hey everybody welcome back to another

play00:01

video on this channel and today we'll

play00:03

talk about the very common Arrow message

play00:05

next instance unavailable I know it can

play00:08

be horrible but let's see what it means

play00:10

how to fix it and let's get rid of it

play00:12

right away here we

play00:15

[Music]

play00:18

go before we dig into that Arrow message

play00:21

and what it means you remember maybe

play00:23

some of you the last video there was a

play00:25

raffle and I have to extend the raffle

play00:27

for yet another week why because well

play00:30

Google on YouTube there were some

play00:31

problems with showing commands so yeah I

play00:35

might have missed a comment or two from

play00:37

one of you because they were not showing

play00:38

up on the page that means you can

play00:40

comment once again under this video also

play00:42

on both that's fine I will just pick out

play00:44

one entry per person uh with why you

play00:47

want to go to viewon and what nice thing

play00:49

you have built with you and or next

play00:51

already just comment down below and then

play00:53

next week the winner will be drawn for a

play00:55

free ticket for vew confs so um if

play00:59

that's your chance if you can travel to

play01:00

the US on uh May 15 16 7s that's the way

play01:04

to go it would be amazing to meet you

play01:06

there and uh yeah good luck best of

play01:08

success and let's jump to the the aror

play01:11

message now and how all of that is about

play01:14

asynchronous code and yeah as I said

play01:17

before KNX inst is unavailable I guess

play01:19

some of you have hit that already here

play01:21

see a screenshot of that Arrow message

play01:22

and we'll see it in a bit already um but

play01:25

first of all let's figure out how to

play01:28

provoke the error message and then work

play01:29

on on how to fix it understand why it

play01:31

actually happens as I mentioned it's a

play01:33

lot about as code so let's dig into our

play01:36

demo application and um break it and

play01:39

then fix it our demo application is

play01:42

pretty minimal as usual we have the KNX

play01:43

config with the black white fix so just

play01:46

a bit of styling and then we have our

play01:48

aped view saying too here and to provoke

play01:51

the error message is not that difficult

play01:52

we need some asynchronous code so let's

play01:54

start with awaiting some use Asing data

play01:56

here and let's just return a new promise

play01:58

which we will resolve IM immediately so

play02:01

let's just put resolve in here and say

play02:03

we resolve with one doesn't matter the

play02:06

value and after that we want to call

play02:09

some extra composable which also needs

play02:11

next instance because otherwise would

play02:13

say it's unavailable um and it say title

play02:16

head is set so we see it in tab title

play02:18

for sure that it's available and if we

play02:20

save that right here and we switch to

play02:22

the browser what will we see all fine it

play02:25

works and actually that's expected

play02:28

because well why should that code work

play02:30

right so how can we break

play02:33

it well let's move the whole code over

play02:36

here from script setup in an extra

play02:39

composable so even though I said yeah

play02:41

you know if you only use it in one

play02:43

component let's not extract it inline

play02:45

composes and so on if you haven't seen

play02:47

the video it's linked in this case let's

play02:49

do that let's go to composable and say

play02:50

use break

play02:52

mecs and let's say okay we export the

play02:55

default Asun function and call use break

play02:58

me

play03:00

all right and then we put the same code

play03:02

we had before in here we Sav that in an

play03:04

app. view we just call use break me all

play03:08

right and if we save this now and check

play03:11

out the website what we will see well

play03:14

you guessed it already 500 KN a

play03:17

composable that requires access to the

play03:19

KN instance was called outside of a

play03:20

plugin KN to KNX middleware VI setup

play03:22

function this is probably not knb well

play03:24

it's not find out more at this and this

play03:28

whole documentation part explains pretty

play03:30

well what uh View and KN composes are

play03:33

what this next instance unavailable

play03:34

means so it's worth checking that out

play03:37

but I mean I'm also here to explain it

play03:38

to you right so let's dig into it and to

play03:41

start with why the error message happens

play03:43

we should take a look at why exactly

play03:45

this already happens in plain view as

play03:47

well so let me let me show you one thing

play03:50

let's move the things back into script

play03:52

setup but without a setup so let's take

play03:55

the content from use break me here and

play03:57

let's remove that setup here and just

play03:59

use the plain old setup function through

play04:03

the options API right like this maybe

play04:06

let's just do this and say we called it

play04:08

a sunc setup so if we take that code and

play04:12

we would refresh our page well we will

play04:15

still see the same arrow with just a

play04:17

different point to point at so the

play04:19

problem is somewhat related to script

play04:22

setup because there seems to work in

play04:24

other files it doesn't and once again to

play04:26

check out why this is the case let's

play04:28

jump into how view handle Asing

play04:30

composable and code then we understand

play04:32

why the works script setup but not in

play04:34

the rest and then we'll fix it the best

play04:37

resource for that is the Asing with

play04:39

composition API blog post from core team

play04:41

member of vi v and KN Anthony Fu on his

play04:45

personal website that explains very well

play04:48

how the whole asynchron works under the

play04:51

hood with View and the main point is

play04:53

that even in view if you have some kind

play04:55

of async

play04:57

function and then have another

play04:59

composable and you evaded ason function

play05:01

you have a problem because view itself

play05:04

needs a component instance right so in

play05:06

here we see that pretty well let me Zoom

play05:08

that bit in so we'll say okay we have

play05:10

some instance right we set the instance

play05:13

as a global variable and we don't want

play05:14

to share that between the requests so we

play05:17

have to delete it after calling the

play05:18

setup right so it's like okay all right

play05:21

get the instance call setup and then

play05:23

restore a possible previous one if you

play05:25

want to so that's how view mounts

play05:27

component and how it stores that

play05:29

instance in a global variable the

play05:32

problem is though if we have some kind

play05:35

of this well this doesn't really work

play05:39

with asynchron right so as you all know

play05:42

you've probably seen all these like

play05:43

console log examples here's another

play05:45

wonderful one showing that yeah um that

play05:49

that won't work so we got not the result

play05:51

we want here and this is more or less a

play05:54

design limitation with composable so

play05:56

it's not like that something is

play05:58

fundamentally broken it's just something

play05:59

something that has to be considered when

play06:01

using composes that's also why a few

play06:03

people um suggest that composes should

play06:07

rarely be asung by default so you can

play06:10

decide if you want to weit them or not

play06:11

and I think it makes sense but back to

play06:14

the topic here let's check out how view

play06:16

solves this with script set up and to be

play06:19

fair there are lots of solutions that

play06:20

antin mentioned here once again you can

play06:21

just say hey let's not do that let's

play06:23

avoid that let's call for example all

play06:24

life cycle Hooks and all composes before

play06:27

the async ones but especially with

play06:29

that's not always easy and possible

play06:31

especially if you say okay there's

play06:33

something depending on something else

play06:34

you still can do that that's not

play06:36

necessarily a problem but that's one

play06:39

option another option would be okay we

play06:41

can do it as reactive sync we skip over

play06:44

that but read the blog post it's really

play06:45

good and the other option would be hey

play06:48

let's explicitly bind the instance but

play06:51

the problem is that doesn't fully work

play06:53

and it's a bit of like internal API so

play06:56

yeah not ideal but the best suggestion

play06:59

is is compile time magic and as we use

play07:01

script setup mainly well that is

play07:04

resolved and let's see how cons poost

play07:07

equals a weight fatch looks like after

play07:09

the compilation you can also check that

play07:10

in a view single file playground link is

play07:14

in the description of the video so here

play07:16

we see okay we have underscore uncore

play07:18

toreore restore and more or less this is

play07:21

called with an with as context function

play07:24

here with a temp and a restore we

play07:27

awaited and then we call the restore

play07:29

function to get get the instance back so

play07:31

if you don't fully grasp it code is

play07:32

totally fine you don't have to it also

play07:34

took me a while but this will work for

play07:36

script setup because the compiler can do

play07:38

some magic so that's great right but as

play07:42

soon as you use composes yourself you're

play07:44

not really into script setup especially

play07:46

nested composes that doesn't fully work

play07:48

anymore same with well anything that you

play07:51

extract so while script setup solves it

play07:53

on top level going levels deeper

play07:56

extracting things composing things will

play07:58

make it a bit more difficult but there's

play08:00

also a solution there especially with KN

play08:03

so let's have a look how that works

play08:05

there as I mentioned with KN we have a

play08:07

couple of solutions so maybe let's

play08:09

change that back here to script

play08:10

setup um maybe bigger not await script

play08:14

setup here and we await use break me

play08:16

okay so so far so good the arrrow is b

play08:19

as before and let's fix that so in here

play08:22

we have exactly this we have the await

play08:25

and then the use head so one way to

play08:27

tackle this is as Anthony said let's

play08:29

just well move things around so we could

play08:33

say let's call use head first and then

play08:36

we await the use Asing data part and

play08:39

that should work fine so let's check in

play08:41

the browser if that's the case and yes

play08:44

we got it of course because now the asnc

play08:46

operation comes last but what if it's

play08:48

different what if the whole thing

play08:50

depends actually on news Asing data so

play08:53

what if we say okay you know what we'll

play08:55

get some data here so let's say cons

play08:56

data equals use it in

play09:00

data and the title should be in here

play09:03

let's say data. value and probably just

play09:06

said data that's fine and we resolve it

play09:09

with title set and also we make sure

play09:11

that it's definitely there so it might

play09:13

not complain it's still complaints

play09:16

because okay yeah we don't know let's

play09:17

type it as any to make it bit

play09:21

happier and

play09:23

see what will happen now but here the

play09:25

problem is now that we can't really

play09:26

await it anymore because if we await it

play09:28

then let's check the browser things will

play09:31

break again and that's not really

play09:33

desired so instead of doing that we

play09:35

could say okay let's return response

play09:37

here and have used as data we don't

play09:40

await it we say Okay response. data is

play09:42

what we need that's the same as before

play09:44

right and then here we return the

play09:46

response still given an option to await

play09:50

so we still awaited in use break me and

play09:52

now things should work pretty fine in

play09:54

the browser and they

play09:56

do interestingly that is still not the

play09:59

most preferred approach here there's

play10:01

still three more options to fix that

play10:03

because also this scenario here it it

play10:06

works and once again you have the option

play10:07

you don't have to keep it Asing you can

play10:10

fully not decide not to you can just

play10:12

call it somewhere so that's pretty cool

play10:14

and you can even say hey use lazy Asing

play10:16

data if you want to and then use pending

play10:17

State and so on so it's pretty nice but

play10:21

there are a few more things so let's

play10:22

just jump back to the await here and

play10:25

let's keep exactly this scenario let's

play10:27

say use head is depending on use Asing

play10:30

data so this once again breaks we've

play10:32

seen that before and there's another

play10:34

option there's another option called run

play10:36

with context so the idea is that we have

play10:39

uh the KN app through use KN app so

play10:41

let's call this and say we have the app

play10:43

here and now we say yeah you know what

play10:46

to not lose the context we can run

play10:48

app.run with context as long as we have

play10:51

the next app beforehand and we just wrap

play10:53

use head in here around so in

play10:57

here this 96 this use hat will run now

play11:01

with the next context as wanted and as

play11:03

set and if we check the browser things

play11:06

work again mainly because the context is

play11:07

now not lost anymore and we also don't

play11:10

have to pass it explicitly to the

play11:12

composable which would be a bit strange

play11:14

and weird so the good news this works

play11:17

fine there's also one more option called

play11:19

call with KN it's basically the same

play11:21

idea but you pass in Call of KN then the

play11:24

KNX app then the function and then the

play11:26

arguments let me quickly show that to

play11:28

you so so instead of run with context

play11:31

you would do call with next you would

play11:33

have to import that it's not Auto

play11:34

imported you would have to pass in the

play11:36

app use hat and then an array with

play11:39

arguments in this case it's just one if

play11:41

we remove this and um do some

play11:44

exclamation marks here and refresh the

play11:47

browser we will see that's also as

play11:49

expected over here so these are two

play11:52

options to resolve that in your code

play11:54

itself to make sure that the whole

play11:55

instance is not lost at all but what if

play11:58

it tell you there's one more option

play12:00

without changing anything that would be

play12:02

cool right let's dive into it and that

play12:06

means let's build everything back as it

play12:08

was before we had a you know what just

play12:10

let's let's just go back we had to use

play12:12

head before with um straight under it

play12:15

there we go we don't need to return

play12:17

anything and that's it and just to make

play12:19

sure once again in the browser this

play12:21

breaks as expected so how we fix it well

play12:25

we can do the following in our nox app

play12:27

we can use experimental and we use as

play12:30

context and set it to true

play12:33

and this little thing allows us to use

play12:37

um an experimental feature called as

play12:39

context that's um what the parameter

play12:42

says too enable native as context to be

play12:44

accessible for n composes and the best

play12:46

part is if we now just check the browser

play12:49

we're good I didn't change anything else

play12:51

in the code let's just quickly check

play12:53

again and write like title set switch

play12:55

back it works awesome but wait first of

play13:00

all what is this as in context and

play13:02

second of all why uh why is it not

play13:04

enabled by default why is it

play13:06

experimental let's check it out best

play13:09

resource for this is the pull request

play13:10

with regards to the experimental native

play13:12

as in context and while we dig into how

play13:15

view handles the ason context topic and

play13:18

not losing it through script setup we

play13:20

didn't check how KN is doing that and

play13:22

that's a good point so puya is nicely

play13:24

listing here how the whole thing works

play13:27

with uh KN as well we have The View uh

play13:30

script setup option here to support

play13:31

Asing KNX uses Asing transform through

play13:34

UNC context UNC context or uncx is yet

play13:37

another package of njs and this also has

play13:41

an Asun context option and also an Asun

play13:43

transform option to make sure that in

play13:46

plugins and middleware everything will

play13:48

be nicely uh wrapped around so you don't

play13:51

have to deal with these contact issues

play13:53

for you so that's also what puya

play13:55

mentions here but the problem is it's

play13:57

one single level

play13:59

because for nest composes it's a bit

play14:01

trickier right so when we see this here

play14:05

yeah this this will not exactly work you

play14:08

can have a look PR is once again Linked

play14:09

In the description below and the problem

play14:11

is that well as soon as you go deeper

play14:14

and deeper no wrapping around will help

play14:16

of course you can still do it manually

play14:18

with KN run context so this works but

play14:21

also just for one level and well the

play14:25

only real solution here would be the as

play14:28

in context

play14:29

so this is a feature based on nodes asy

play14:32

local storage and that's an important

play14:34

point I'll come to that that in a bit

play14:36

and un context to make sure okay as

play14:39

context will work out of the box for all

play14:41

nested as composes which is great you

play14:43

don't have to transform anything and so

play14:46

on and so on but the problem is that

play14:48

noes Asun storage well it's only for

play14:52

node it's it's not necessary support in

play14:54

all run times and while anjs no and

play14:57

Nitro and so on we all try try to be

play14:59

runtime agnostic this is experimental so

play15:02

it might not work in some production

play15:04

platforms but in death it seems fine

play15:06

which is a bit meh but luckily wherever

play15:09

node itself runs that's a good chance it

play15:11

will work and the good part is there's a

play15:14

mock for that in unen which is also used

play15:16

so you can definitely try it out but

play15:19

there's more good news as mentioned this

play15:21

whole feature is built based on noes

play15:24

Asun local storage but there is a

play15:27

proposal from tc39 for quite a while

play15:29

already and the idea is to bring as

play15:32

context as a language primitive in here

play15:34

and the good thing is also it's on stage

play15:36

two so it's not super super far away

play15:38

given that you can start with Stage zero

play15:40

which is any kind of proposal in stage

play15:42

one we like okay let's get it slowly

play15:44

going but it can still mean couple of

play15:46

years we don't know yet so keep track

play15:49

take a look at as context and the idea

play15:51

is here is that if you have it in

play15:53

JavaScript itself you don't really need

play15:55

to rely on node or Deno or any kind of

play15:57

other kind of runtime it will just work

play15:59

everywhere where JavaScript is supported

play16:01

and that would be pretty pretty neat but

play16:03

yeah the idea is once again it's limited

play16:06

for now for now there is also cloudflare

play16:09

workers who also have as context

play16:10

tracking so there are few ways to take a

play16:13

look if that simple flipping uh the

play16:16

switch would work out for you especially

play16:18

if you host things not on like an edge

play16:21

or an um serverless runtime there might

play16:23

be good chances even serverless with um

play16:25

with just node could work so flip it on

play16:28

try it against your cicd and I I hope

play16:31

you have you have cicd right otherwise

play16:33

there's a video about testing at Le some

play16:35

basic tests can help also catching these

play16:37

so check it out um and there you go so

play16:42

let's give a very brief summary so first

play16:43

of all this error message is basically

play16:45

related to asyn code that the next

play16:48

instance the context of it is not

play16:50

available anymore there is as I said

play16:52

before good documentation which is

play16:53

linked below as well and I've briefly

play16:55

shown how to provoke the error message

play16:56

how to prevent it and we have couple of

play16:59

ways to deal with it from not using

play17:01

async weight straight away so from

play17:04

rearranging things to make the composes

play17:06

come before the weed uh composable or

play17:09

function to using call with KN and app.

play17:12

run with context from use KN app and

play17:15

last but not least the experimental Asin

play17:17

context option so let me know have you

play17:20

had that error message I'm pretty sure

play17:21

you had because there's so many of that

play17:24

let me know how you fixed it how it

play17:25

worked out and which option of these

play17:27

will you try and which worked for you

play17:30

let me know in the comments and uh as

play17:32

usual check also the newest D of view

play17:34

episode with Michael and me talking

play17:35

about teleports in View and also an so

play17:38

don't forget to check that out other

play17:40

than that questions suggestions

play17:42

feedbacks topics everything you want

play17:44

down there um also thanks a lot to all

play17:47

the channel members and um see you guys

play17:50

and girls next Friday talk to you soon

play17:53

happy hacking

play17:57

[Music]

play17:59

oh

play18:02

[Music]

Rate This

5.0 / 5 (0 votes)

Related Tags
Vue错误异步代码代码修复前端开发技术教程JavaScript编程技巧Vue.js错误处理教程视频
Do you need a summary in English?