Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
AsyncStorageをNodeのREPLから操作するためにTop-levelでawaitし...
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
joe_re
July 14, 2017
Technology
530
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
AsyncStorageをNodeのREPLから操作するためにTop-levelでawaitしたい人生だった
五反田.js #8 LT資料
joe_re
July 14, 2017
More Decks by joe_re
See All by joe_re
Eyes on Claude Code
joere
0
140
Building Public API with GraphQL
joere
3
150
Traversing the GraphQL AST and Calculating Query Costs
joere
0
1.3k
Real-Time applications with GraphQL
joere
0
310
Prisma2 with Graphql
joere
3
1k
Go beyound static on Netlify
joere
1
380
Building Real-time Vue App
joere
4
4.7k
ReactNativeのAsyncStorageをNodeのReplから操作する
joere
0
360
Mock Native API in your E2E test
joere
2
1.2k
Other Decks in Technology
See All in Technology
データ組織の転換期 一足飛びしない段階的戦略
leveragestech
PRO
0
130
運用を犠牲にせずコストを制御し事業成長を支える B2B SaaS ID管理基盤におけるS3 Tableのログストレージ活用
kaminashi
1
130
なぜ、あなたのエージェントは言うことを聞かないのか
segavvy
1
610
「待ち時間」の消滅と「自我消耗」の加速:生成AI時代のエンジニアを救うメンタル・リソース管理
poropinai1966
0
390
PLaMo 3.0 Primeの構造化出力サポート
pfn
PRO
0
150
Retriever と Reranker、結局どうする?
kazuaki
1
560
Amazon Bedrock Managed Knowledge BaseDive Deep
ren8k
0
320
歴史から理解するクラウドインフラのしくみ
kizawa2020
0
200
オートマトンと字句解析でRoslynを読む
tomokusaba
0
130
DevOps Agentで運用判断をチーム資産にする~Agent InstructionsとAgent Skillを継続的に育てる~
fujioka6789
0
180
AIで楽になるはずが、なぜ疲れる?
kinopeee
0
150
Escolhendo LLMs na Prática: Lições Reais em Busca Agêntica no Mercado Livre —TDC 2026 Floripa
jpbonson
0
110
Featured
See All Featured
Discover your Explorer Soul
emna__ayadi
2
1.2k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Ruling the World: When Life Gets Gamed
codingconduct
0
290
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
680
Marketing to machines
jonoalderson
1
5.6k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
The Curious Case for Waylosing
cassininazir
1
440
Being A Developer After 40
akosma
91
590k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Transcript
AsyncStorage をNode の REPL から操作するために Toplevel でawait したい 人生だった @joe_re
Who am I? twitter: @joe_re github: @joere working in freee.K.K
What is AsyncStorage? ReactNative の標準で搭載されたkeyvalue ストレージ 気軽にReactNative に永続化したいデータを持つときに便利 ReactNative で使えるLocalStorage
みたいなもの
Example Persisting date: try { await AsyncStorage.setItem('@MySuperStore:key', 'I like to
save it.'); } catch (error) { // Error saving data } Fetching date: try { const value = await AsyncStorage.getItem('@MySuperStore:key'); if (value !== null){ // We have data!! console.log(value); } } catch (error) { // Error retrieving data }
AsyncStorage をNode のREPL から操作したいという欲望 AsyncStorage には外部から叩けるAPI は用意されていない 開発中にデータ構造が変わったときにアプリケーションに データを修正するためのコードを書くのは面倒 ちゃんとデータが保存されたかどうか確かめるのも面倒
REPL からAsyncStorage のAPI を叩きたい (rails console みたいなイメージ)
作った https://github.com/joere/asyncstoragerepl
DEMO
仕組み Node とReactNative との通信はWebsocket で行う REPL 起動時に子プロセスとしてReactNative アプリケーションと通信するWSS を立ち上げる REPL
とWSS はプロセス間通信を行う
イメージ図
ん?
どうしてこうなった
Node.js ではTop Level で非同期処理の 結果を扱えない 人類はES2017 よりasync/await を獲得したが、 await はasync
function の中でしか使えない // ng function request { const response = await fetch( '/data.json' ); const json = await response.json(); const result = await hydrateSomehow(json); return result; } // ok function async request { const response = await fetch( '/data.json' ); const json = await response.json(); const result = await hydrateSomehow(json); return result; }
REPL で Promise オブジェクトを 扱おうとすると.. > let out = null;
> promiseObj.then((res) => { out = res }); > out // finally available here
だるすぎワロた
Toplevel await is a footgun https://gist.github.com/Rich Harris/0b6f317657f5167663b493c722647221 Toplevel のawait を許すと1
つの依存を解決するだけでも、 その先の関係のない処理まで止めざるを得ない await は直列を強要するので、ネットワークを介した処理の 並列化ができない
Example(borrow from Harris) // main.js import './foo.js'; import './bar.js'; //
foo.js await delay( Math.random() * 1000 ); console.log( 'foo happened' ); // bar.js await delay( Math.random() * 1000 ); console.log( 'bar happened' );
将来的にはToplevel await が できるかも? https://github.com/tc39/ecmascriptasyncawait/issues/9
それはそれとしてNode のREPL に 例外的に入る可能性も https://github.com/nodejs/node/issues/13209
今回はこのような実装に _resolveMessageFromRN(fileName) { let json = {}; let received =
false; for (let i = 0; i < this.timeout; i++) { sleep.msleep(100); const fileContent = fs.readFileSync(fileName, 'utf8'); if (fileContent) { json = JSON.parse(fileContent); delete this.que[Number(json.queId)]; received = true; break; } sleep.msleep(900); } fs.unlinkSync(fileName); if (!received) { throw new Error("can't receive response from ReactNative Application"); } if (json.error === 1) { throw new Error(json.message); } return json.result; }
そうは言っても結構便利です。 ReactNative のお供にぜひ!
ありがとうございました