{"record":{"id":"a76401fa0efa863a","repo":"appsmithorg/appsmith","slug":"onready-expects-a-function-as-parameter","errorCode":null,"errorMessage":"onReady expects a function as parameter","messagePattern":"onReady expects a function as parameter","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/client/src/widgets/CustomWidget/component/customWidgetscript.js","lineNumber":287,"sourceCode":"        },\n        triggerEvent: (eventName, contextObj) => {\n          if (typeof eventName !== \"string\") {\n            throw new Error(\"eventName should be a string\");\n          } else if (contextObj && typeof contextObj !== \"object\") {\n            throw new Error(\"contextObj should be an object\");\n          }\n\n          // Send a trigger event message to the parent\n          channel.postMessage(EVENTS.CUSTOM_WIDGET_TRIGGER_EVENT, {\n            eventName,\n            contextObj,\n          });\n        },\n        model: {},\n        ui: {},\n        onReady: (fn) => {\n          if (typeof fn !== \"function\") {\n            throw new Error(\"onReady expects a function as parameter\");\n          }\n\n          onReady = fn;\n\n          if (isReady && !isReadyCalled && onReady) {\n            onReady();\n            isReadyCalled = true;\n          }\n        },\n      },\n    });\n  }\n\n  // Listen for the 'load' event and send READY message to the parent\n  window.addEventListener(\"load\", () => {\n    channel.postMessage(EVENTS.CUSTOM_WIDGET_READY);\n  });\n}","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/appsmithorg/appsmith/blob/8cd9021c24cdbea1c3c12c966073708e83db60c2/app/client/src/widgets/CustomWidget/component/customWidgetscript.js#L269-L305","documentation":"Thrown by window.appsmith.onReady() in a legacy Custom Widget iframe. onReady registers a single callback that fires once the Appsmith host signals readiness; if the host is already ready and onReady has not been called yet, the callback is invoked synchronously. A non-function argument is rejected because the method immediately assigns onReady = fn and may call it.","triggerScenarios":"Calling appsmith.onReady(undefined), appsmith.onReady(null), appsmith.onReady('init'), appsmith.onReady({init:fn}), or appsmith.onReady() with no argument. Calling onReady twice with the second arg accidentally omitted.","commonSituations":"Invoking an init function instead of passing it (onReady(init())); passing a config object that contains the handler; migrating from a Promise-based API and passing onReady(resolve).","solutions":["Pass the function reference: appsmith.onReady(init).","Use an inline arrow: appsmith.onReady(() => { /* bootstrap */ }).","Keep onReady idempotent and lightweight since only the last registered callback is retained.","Perform side-effecting bootstrap inside the callback, not at module top-level, to ensure the host channel is ready."],"exampleFix":"// before\nappsmith.onReady(bootstrap()); // runs bootstrap, passes undefined\n\n// after\nappsmith.onReady(bootstrap); // passes the function reference","handlingStrategy":"type-guard","validationCode":"if (typeof onReadyHandler !== 'function') {\n  throw new TypeError('onReady expects a function');\n}\nappsmith.onReady(onReadyHandler);","typeGuard":"const isCallable = (v) => typeof v === 'function';\n\n// usage\nif (isCallable(bootstrap)) appsmith.onReady(bootstrap);","tryCatchPattern":"try {\n  appsmith.onReady(bootstrap);\n} catch (e) {\n  console.error('onReady registration failed:', e.message);\n}","preventionTips":["Pass the function reference; never invoke it at the call site.","Keep the callback idempotent because only the last registered callback is retained.","Do side-effecting bootstrap inside the callback, not at module top-level.","Lint for appsmith.onReady(\\w+\\(\\)) to catch accidental invocations."],"tags":["custom-widget","appsmith-api","parameter-validation","javascript"],"backgroundTag":null,"analyzedSha":"8cd9021c24cdbea1c3c12c966073708e83db60c2","analyzedAt":"2026-08-12T22:14:19.293Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}